Module 9: Object-oriented programming
Model the real world with classes: objects, encapsulation, inheritance, polymorphism and good object design.
- Classes and objects: A class is a blueprint; init fills each new object through self; print uses str, lists and the shell use repr.
- Working with objects: Class attributes are shared, self.x is per object; eq/lt make objects comparable and sortable; @classmethod gets cls (alternative constructors), @staticmethod gets nothing.
- Encapsulation: x means internal, x is name-mangled to Classx; @property (plus @x.setter) guards an attribute while keeping obj.x syntax.
- Inheritance: class Child(Parent) reuses and overrides; super() calls the next class in the MRO; check the order with Cls.mro().
- Abstraction and polymorphism: Polymorphism and duck typing: same call, each object answers its own way; ABCs force subclasses to implement methods; no overloading (the last def wins), but operators overload through dunders.
- Object creation and design: new creates and returns the object, init fills it; slots fixes attributes; @dataclass writes the boilerplate; Enum names constants; Singleton, Factory and Observer are everyday patterns.
Ends with a 14-question checkpoint (80% passes) and a mini project: Canteen Counter v2: Menu and Order classes.