D

OOP

• Encapsulation: hiding internal state, using methods to control access (getters/setters)

• Abstraction: hiding complexity, exposing only essential features

• Class: blueprint for creating objects, defines attributes and methods

• Object: instance of a class, created with object_name = ClassName()

• Constructor: init method, initializes object attributes when class is instantiated

• Self: refers to the instance of the class, used to access variables and methods inside class

• Method: function defined within a class, takes self as the first argument

• Attribute: variable associated with an object or class, accessed via dot notation

• Class Variable: shared across all instances of a class

• Instance Variable: unique to each instance, usually defined in init

• @classmethod: method that takes cls as first parameter, works with class not instance

• @staticmethod: method that doesn’t take self or cls, acts like a regular function inside a class

• Inheritance: acquiring properties from a parent class

• Method Overriding: redefining a method from the parent class in the child class

• Duck Typing: “If it walks like a duck and quacks like a duck…” — focus on behavior, not type

Three most complex ideas in Python OOP:

1. Multiple Inheritance and MRO (Method Resolution Order)

Understanding how Python resolves method calls when a class inherits from multiple parents can be tricky. The super() function combined with the C3 linearization algorithm adds a layer of complexity.

2. Descriptors and the get, set, delete Protocols

These power how attributes work under the hood, including @property and class-level behavior like static/class methods. They’re abstract and not often used directly, but fundamental to Python’s object model.

3. Metaclasses

Classes that define how other classes behave. Metaclasses can modify or generate classes at runtime, which is powerful but not intuitive—essentially “classes of classes.

Three most common beginner mistakes:

1. Misunderstanding self

Beginners often forget to include self in method definitions or misunderstand what it represents—it refers to the specific instance, not the class itself.

2. Confusing Class vs Instance Variables

Many mix up class variables (shared across instances) with instance variables (unique per object), leading to unexpected behavior especially when mutable types like lists are involved.

3. Using Inheritance Instead of Composition

New learners often default to inheritance even when composition would be clearer or more flexible, which can lead to rigid and tightly coupled code.

Want a visual or code example for any of these?