PYTHON FUNDAMENTALS: PROGRAMMING FOUNDATIONS / L10INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
LESSONS · 12 · 10 / 12
LESSON 10 · BEGINNER · 45 MIN · ◆ 3 INSTRUMENTS

Introduction to Object-Oriented Programming

Understand classes, objects, attributes, and methods. Build your first Python classes.

TIP

Learning Objectives: After this lesson, you'll understand the fundamentals of OOP in Python - classes, objects, attributes, and methods - and learn to build your first Python classes with proper encapsulation.

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm that organizes code around objects - entities that contain both data (attributes) and functions (methods) that operate on that data.

Think of objects like real-world entities:

  • A car has properties (color, model, speed) and behaviors (start, stop, accelerate)
  • A bank account has properties (balance, owner) and behaviors (deposit, withdraw, check_balance)
  • A student has properties (name, grades) and behaviors (study, take_exam, calculate_gpa)

The fastest way to feel this is to build an object and call its methods yourself. Use the playground below before we dive into syntax:

FIG. 02OOP Playground
INTERACTIVE
LOADING INSTRUMENT
Fig. 02Interactive playground for learning OOP concepts

Try it: create two accounts, then call deposit on just one of them and watch each object keep its own balance — that independent per-object state is the whole point of OOP.

FIG. 04Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 04Interactive Python code execution environment

Classes and Objects

A class is a blueprint for creating objects. An object is an instance of a class.

FIG. 06Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 06Interactive Python code execution environment

Interactive OOP Playground

Now that you've seen the syntax, revisit the OOP Playground at the top of the lesson — try adding a method and watch how each object maintains its own state independently.

Class Anatomy

Let's break down the parts of a class:

FIG. 08OOP Inheritance Tree
INTERACTIVE
LOADING INSTRUMENT
Fig. 08Interactive visualization of class inheritance
FIG. 10Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 10Interactive Python code execution environment

Instance vs Class Variables

Understanding the difference between instance and class variables:

FIG. 12Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 12Interactive Python code execution environment

Methods and Self

The self parameter is crucial in Python classes:

FIG. 14Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 14Interactive Python code execution environment

Encapsulation and Privacy

Python uses naming conventions to indicate privacy:

FIG. 16Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 16Interactive Python code execution environment

Special Methods (Magic Methods)

Python classes can define special methods that enable built-in operations:

FIG. 18Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 18Interactive Python code execution environment

Practical Examples

Example 1: Student Management System

FIG. 20Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 20Interactive Python code execution environment

Example 2: Library Management System

FIG. 22Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 22Interactive Python code execution environment

Key Takeaways

Classes are blueprints for creating objects with attributes and methods
Objects are instances of classes with their own data
init method initializes new objects (constructor)
self parameter refers to the current instance
Instance variables are unique to each object
Class variables are shared by all instances
Encapsulation uses naming conventions (_protected, __private)
Special methods enable built-in operations (__str__, __len__, etc.)

Connections: Where OOP Goes Next

You've now got the core: classes, objects, self, instance vs class variables, encapsulation, and magic methods. Here's how that foundation extends into the rest of programming — each of these is a topic to explore once the basics feel solid.

  • Modeling the real world. Classes map onto things ("has-a" composition: a Car has an Engine) and types ("is-a" inheritance: a Dog is-a Animal). Picking the right relationship is most of good design.
  • Other paradigms. Procedural keeps data and functions separate; OOP bundles them; functional favors pure functions and immutable data (@dataclass(frozen=True)). Real code mixes all three.
  • Design principles & patterns. The SOLID principles and patterns like Factory, Singleton, and Observer are reusable solutions to recurring class-design problems — worth studying after you've written a few classes of your own.
  • Across languages. The concepts transfer: __init__ is Java/C++'s constructor, class Dog(Animal): is extends/: public. Python's distinctives are duck typing, multiple inheritance (with MRO), and explicit self.
  • Data structures. Linked lists, trees, and graphs are all built from small classes (a Node holding data plus references to other nodes).
  • Performance. Each instance normally carries its own __dict__; __slots__ trades flexibility for ~50% less memory per instance when you have many objects.
  • Testing & architecture. Objects make code easy to mock (swap a real EmailService for a fake one in tests) and scale up to microservices and REST APIs, where each service/resource behaves like an object with clear responsibilities.

Remember: OOP isn't just syntax — it's a way of thinking about problems. Model your programs after the real world, and your code becomes more intuitive, maintainable, and powerful.

Next Steps

In the next lesson, we'll learn about error handling and debugging - how to handle errors gracefully with try-except blocks, understand common exceptions, and master debugging techniques to make your code more robust.


Ready to make your code bulletproof? The next lesson will teach you professional error handling techniques!


Further Reading

Visualize It

  • Python Tutor — pasting a class with two instances shows how self binds, where attributes live (instance dict vs class dict), and what __init__ actually does.

Official Docs

Tutorials

Modern Python

  • Pydantic v2 — when your "data classes" need validation. The standard for API/config models.
  • attrs — what dataclasses are based on; offers more features (__slots__, validators, converters).
  • functools.cached_property — memoize expensive instance properties cleanly.

Books

  • Book: Fluent Python — Chapters 5 ("Data Class Builders"), 11 ("A Pythonic Object"), 14 ("Inheritance: For Better or for Worse"). The definitive treatment.
  • Book: Effective Python — Items 37–43 cover class design.
CONNECTED CONCEPTS
oopclassesobjects