When learning Python and exploring object-oriented programming (OOP), beginners often encounter something that looks like a function but behaves differently:
def __init__(self, ...):
At first glance, it seems like we’re just defining a function inside a class. But what’s really going on? Why doesn’t this behave like a regular function like print()
or len()
? Let’s break it down.
🔸 Is __init__
a Function?
Yes — __init__
is a function, but it’s a special kind of function in Python known as a dunder method (short for “double underscore”).
More specifically, __init__
is a constructor. It’s automatically called when a new object is created from a class. You can think of it as a setup function that runs only once when you create an object.
🔹 Example: Defining a Class With __init__
class Person:
def __init__(self, name, age):
print("Initializing a new Person object...")
self.name = name
self.age = age
Now when you create a new Person
object:
p1 = Person("Alice", 30)
This automatically triggers:
__init__(self=p1, name="Alice", age=30)
Which sets:
p1.name = "Alice"
p1.age = 30
🔸 How __init__
Differs From Regular Functions
Let’s compare it to a built-in function like print()
:
Feature | print() | __init__() |
---|---|---|
Where it lives | Globally available | Inside a class |
When it’s called | When you type print(...) | Automatically, when you create an object |
Purpose | Display information | Initialize object data |
Defined by | Python’s standard library | You (in your class definition) |
🔹 Each Class Has Its Own __init__
Unlike built-in functions which are defined once and reused globally, each class in Python defines its own version of __init__
, tailored to what that class represents.
✨ Example: Two Different __init__
Methods
class Dog:
def __init__(self, breed):
self.breed = breed
class Car:
def __init__(self, brand, year):
self.brand = brand
self.year = year
Usage:
d = Dog("Labrador")
c = Car("Toyota", 2020)
print(d.breed) # Labrador
print(c.brand) # Toyota
print(c.year) # 2020
The Dog
class and Car
class both have their own custom initialization logic, defined by their own __init__
methods.
🔁 Summary
Concept | Meaning |
---|---|
__init__ | Special method to initialize a new object |
Defined per class | Yes, each class has its own __init__ |
Called automatically | Yes, when you create an object like obj = Class() |
Purpose | Set up the initial state of the object |
Difference from print() | print() is global and reused; __init__ is class-specific |
🧠 Final Thoughts
The __init__
method may look like just another function, but it plays a key role in Python’s object-oriented design. Think of it as the birth script for every object — customizing how it starts its life. And unlike regular functions like print()
or input()
, __init__
is yours to define inside each class, making it incredibly flexible and powerful.
Want to practice? Try creating your own class with __init__
and explore how object data is stored and accessed. It’s one of the most fundamental skills in Python programming!
Discover more from Aiannum.com
Subscribe to get the latest posts sent to your email.
Leave a Reply