📘 Object-Oriented Python : Core Concepts
▾🔹 Classes & Objects
Class: A blueprint for creating objects. It defines attributes (data) and methods (functions).
Object: An instance of a class. Each object has its own state and behavior.
class Dog: def __init__(self, name): self.name = name
🔹 Inheritance
Allows a class to inherit attributes and methods from another class (parent/child).
Single inheritance: child inherits from one parent.
Multiple inheritance: child inherits from multiple parents.
class Cat(Animal): pass
🔹 Polymorphism
Ability of objects of different types to respond to the same method call in their own way.
Method overriding: child class redefines a method from the parent.
Duck typing: "if it walks like a duck and quacks like a duck, it's a duck".
🔹 Encapsulation
Bundling data and methods that operate on that data within a single unit (class).
Access modifiers: public (default), protected (_single underscore), private (__double underscore).
self._protected # convention
🔹 Special (Magic) Methods
Methods with double underscores (dunder) that allow customizing behavior.
| Method | Purpose |
|---|---|
__init__(self) | Constructor — initializes object |
__str__(self) | String representation for users |
__repr__(self) | String representation for developers |
__len__(self) | Returns length (for len()) |
__add__(self, other) | Overloads + operator |