week 3


Week 3: Object-Oriented Programming & Monkey Patching

1. Classes, Objects, and Constructors

1. Basic Class and Object

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return f"Hello, my name is {self.name} and I am {self.age} years old."

p1 = Person("Alice", 25)
print(p1.greet())

2. Default Constructor

class Car:
    def __init__(self, brand="Toyota"):
        self.brand = brand

c1 = Car()
print(c1.brand)  # Output: Toyota

3. Class with Class Variable

4. Modifying Instance Variables


2. Inheritance, Polymorphism, Encapsulation

5. Single Inheritance

6. Multiple Inheritance

7. Encapsulation (Private Variables)

8. Polymorphism (Method Overriding)


3. Magic Methods (__str__, __repr__, __call__)

9. __str__ Method

10. __repr__ Method

11. __call__ Method


4. Monkey Patching

12. Monkey Patching a Method

13. Monkey Patching an Instance Method

14. Adding a New Method Dynamically

15. Monkey Patching a Function in a Module

16. Patching print Function


5. More Advanced OOP Concepts

17. Static Methods

18. Class Methods

19. Property Decorator (@property)

20. Abstract Classes


Last updated