📘 How does object-oriented Python work?
A class defines a new type by grouping data (attributes) and behavior (methods) under one
What you’ll learn
- Classes, Instances, and StateDefine classes that bundle data and behavior, and distinguish instance state from class-level state using __init__ and self.A class is a blueprint; calling it produces an instance with its own namespace. The __init__ method initializes per-instance state, and self is the conventional name for the bound instance passed automatically to methods. Instance variables hold data unique to each object, while class variables are shared across all instances, which matters most when the shared value is mutable. Attribute lookup checks the instance namespace first and falls back to the class.
- Encapsulation: Properties and Naming ConventionsControl access to object state using naming conventions, name mangling, and the @property descriptor to add validation without changing the public interface.Python relies on convention for encapsulation: a single leading underscore signals 'non-public,' while a double-leading-underscore name triggers name mangling to _ClassName__name to avoid accidental clashes in subclasses. The @property decorator turns method-based access into attribute syntax, letting you add validation or computed values behind a stable interface. Properties are built on the descriptor protocol, which intercepts attribute access through __get__ and __set__. This lets you start with plain attributes and add logic later without breaking callers.
- Inheritance, super(), and Method Resolution OrderBuild class hierarchies that override and extend behavior using inheritance and super(), and explain how Python resolves methods via the C3 MRO.Inheritance lets a derived class reuse and override a base class's behavior; in Python all methods are effectively virtual, so an overridden method is found dynamically. super() delegates to the next class in the method resolution order, enabling cooperative extension rather than hardcoding a base class name. With multiple inheritance, Python computes the MRO using the C3 linearization, which preserves each class's local ordering, visits each ancestor once, and is monotonic. Reading __mro__ reveals the exact search order that drives both attribute lookup and super().
- Special Methods and Pythonic Object BehaviorImplement dunder methods so custom objects integrate with Python's built-in operations like printing, equality, ordering, and hashing.Special ('dunder') methods let your objects respond to built-in syntax and functions: __repr__ and __str__ control textual representation, __eq__ defines value equality, and the rich-comparison methods define ordering. Python's data model imposes a critical contract: objects that compare equal must share the same hash, and overriding __eq__ without __hash__ makes instances unhashable. This is the 'data model' that powers Python's consistency, letting user types behave like built-ins. Implementing these correctly is what makes a class feel idiomatic and safe to use in sets, dicts, and sorting.
- Capstone: Build a Mini Object-Oriented LibraryApply classes, encapsulation, inheritance, abstract base classes, and dataclasses to build and self-critique a small object-oriented artifact.This capstone has you design a small domain model that exercises every prior concept: a class hierarchy with an abstract base class defining the contract, concrete subclasses using super() and overrides, encapsulated state with a validating property, and a value object using @dataclass for concise correctness. Abstract base classes via the abc module make the contract explicit and prevent instantiating incomplete subclasses, while dataclasses auto-generate __init__, __repr__, and __eq__ and handle mutable defaults safely with field(default_factory=...). You will then run a peer-style critique against an objective rubric. The result is a coherent artifact demonstrating idiomatic, accurate object-oriented Python.
Questions this course answers
What is the role of __init__ in a Python class?
__init__ is an initializer that runs after the instance is created, to set up its state; per the data model it must not return a non-None value. Allocation/construction is the role of __new__, not __init__.
Inside the class body you write history = [] and in a method call self.history.append(item). Why might two instances end up sharing one list?
history defined in the class body is a class variable shared across instances; mutating it through any instance affects all. The fix is to assign self.history = [] inside __init__ so each instance owns its own list.
Which statement about self is accurate per the official documentation?
The docs state self has 'absolutely no special meaning to Python' it is a convention. The call x.f() is equivalent to MyClass.f(x), passing the instance as the first argument to every instance method.
Inside class Mapping, the attribute self.__cache is stored under what mangled name?
Name mangling replaces __cache with _classname__cache using the current class name, yielding _Mapping__cache. This prevents accidental clashes with subclass attributes of the same name.
What is the primary purpose of double-underscore name mangling?
The docs state mangling is 'designed mostly to avoid accidents' specifically clashes between a class's private name and one defined by a subclass. It does not provide true privacy; the value can still be accessed via the mangled name.
What does the @property decorator allow you to do?
@property exposes a method as an attribute (no parentheses) and, with a paired setter, lets you validate assignments while keeping the public name unchanged. It is implemented via the descriptor protocol.
Grounded in trusted sources
- Python Tutorial / Language Reference — classes and special methods, https://docs.python.org/3/tutorial/classes.html
- Luciano Ramalho, Fluent Python — data model and OOP idioms
- Brett Slatkin, Effective Python — class and inheritance practices
- PEP 8 — naming conventions for public/protected attributes
- Raymond Hettinger talks / MRO explanations (Python docs glossary)
Every Wunder lesson is built from real, reputable sources — never invented.
Related courses
Wunder is a personalized learn-anything platform — tell it any topic and it builds a beautiful, fact-checked course in minutes, with narration, a knowledge check, and a college-style University track.
© 2026 Wunder Learning LLC · Terms & Privacy