Python Mastery – From Basics to Brilliance / Object-Oriented Programming (OOP)

Constructors (__init__)

A constructor is a special method in a class that runs automatically when an object is created.

It is used to initialize the object’s properties.

In Python, the constructor is the __init__() method.


🧠 Syntax Reminder:

def __init__(self, ...):
    # initialize attributes

💡 Example:

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

p = Person("Alice", 30)
print(p.name)  # Output: Alice

✅ self refers to the current object (just like this in other languages).


🔍 Real-life Analogy:

The constructor is like the setup crew preparing your hotel room before you check in — it gets everything ready when the "object" (you) arrives.