Polymorphism allows objects of different classes to respond to the same interface. In Python, this is implemented through dunder (double underscore) methods—special methods that enable custom classes to integrate seamlessly with Python's built-in syntax and functions.

: Real-world application is reinforced through various projects, such as designing systems for time zones or transaction tracking. Python 3: Deep Dive (Part 4 - OOP) - Udemy

ABCs require explicit inheritance. A class must explicitly inherit from the ABC or register with it to be considered a subclass.

Implement __str__ (for users) and __repr__ (for developers) for better debugging.

| Technique | Benefit | When to use | |------------------------------------|-------------------------------------------|------------------------------------------| | __slots__ | Reduces memory, faster attribute access | Many instances, fixed attributes | | @dataclass(frozen=True) | Immutable, auto __init__ , __repr__ | Data containers without logic | | weakref for cycles | Prevents memory leaks | Observer patterns, caches | | __new__ override | Control instance creation (e.g., singleton)| Rare; use module‑level global instead |

Mastering OOP in Python 3 requires understanding how the language manages memory, organizes class hierarchies via MRO, and utilizes dunder methods to create intuitive, powerful code. Protect data using _ and __ .

Python manages attribute lookups through a specific hierarchy: __getattribute__ , descriptors, the instance __dict__ , the class __dict__ , and finally __getattr__ . Understanding descriptors allows you to intercept and modify this lookup pipeline cleanly. The Descriptor Protocol

By declaring __slots__ , you tell Python to allocate a fixed amount of space for a static set of attributes. This flattens the instance down to an array-like structure in C, eliminating __dict__ entirely.

print(D.__mro__) # (<class 'D'>, <class 'B'>, <class 'C'>, <class 'A'>, <class 'object'>)