OOPs in Python - Cheat Sheet
1. What is OOP?
Object-Oriented Programming is a paradigm based on the concept of 'objects', which contain data and methods.
It helps structure code in a reusable and modular way.
2. 4 Pillars of OOP
1. Encapsulation: Wrap data and methods into a class, with access control.
2. Abstraction: Hide complex implementation and show only necessary parts.
3. Inheritance: One class can inherit properties/methods from another.
4. Polymorphism: Same method name behaves differently in different classes.
3. Class & Object
class Car:
def __init__(self, brand):
self.brand = brand
car1 = Car('Toyota')
print(car1.brand)
4. Constructor (__init__)
Special method to initialize object attributes. Called automatically when object is created.
5. self Keyword
Refers to the current instance of the class. Must be the first argument of instance methods.
6. Instance vs Class Variables
Instance Variable: Belongs to the object (self.var).
Class Variable: Shared across all instances (ClassName.var).
7. Instance, Class, and Static Methods
- Instance Method: Regular method with self.
- Class Method: Uses @classmethod and takes cls as parameter.
- Static Method: Uses @staticmethod. No self or cls.
8. Encapsulation
Hide variables using single (_) or double (__) underscores.
Access through methods (getters/setters).
OOPs in Python - Cheat Sheet
9. Abstraction
Use ABC (Abstract Base Classes) and @abstractmethod to define interface without implementation.
10. Inheritance
class Parent:
def greet(self): print('Hi')
class Child(Parent): pass
c = Child(); c.greet()
11. Types of Inheritance
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
12. super() Function
Calls the parent class's method.
Useful to avoid code duplication in child classes.
13. Polymorphism
Method Overriding: Redefining parent class method in child class.
Method Overloading: Python doesn't support it directly, simulate with default args.
14. Duck Typing
Python supports duck typing: if it walks like a duck and quacks like a duck, it's a duck.
Focus on methods rather than the object type.
15. Operator Overloading
Use dunder methods like __add__, __sub__, etc.
class Point:
def __add__(self, other): return Point(self.x + other.x)
16. Magic Methods (Dunder Methods)
- __init__: Constructor
- __str__: Printable string
OOPs in Python - Cheat Sheet
- __repr__: Debug string
- __len__, __add__, __eq__, etc.
17. Composition vs Inheritance
Inheritance: 'is-a' relationship.
Composition: 'has-a' relationship.
Composition uses objects of other classes as attributes.
18. Access Modifiers
- Public: var
- Protected: _var (convention)
- Private: __var (name mangling)
19. __str__ vs __repr__
__str__: User-friendly string
__repr__: Developer-friendly string (should be unambiguous)
20. isinstance() and issubclass()
- isinstance(obj, Class): Check if obj is instance of Class
- issubclass(Sub, Parent): Check if Sub inherits Parent
21. Aggregation
A form of composition where both objects can exist independently.
class Engine: pass
class Car:
def __init__(self, engine):
self.engine = engine
22. Namespace & Scope
- Class namespace: Shared by all objects
- Instance namespace: Specific to each object