I’m a dedicated dentist passionate about creating healthy, confident smiles. I focus on gentle care, preventive dentistry, and helping every patient feel comfortable during their dental journey.
The __init__() method is a special method (also called a “dunder” method) in Python classes. It automatically runs every time you create a new object from a class.
It’s used to initialize object attributes — i.e., set up the object with specific data when it’s created.
Basic Example
class Person:
def __init__(self, name, age): # constructor
self.name = name # instance attribute
self.age = age
# create object (this calls __init__ automatically)
p1 = Person("Alice", 25)
p2 = Person("Bob", 30)
print(p1.name) # Alice
print(p2.age) # 30
When you call Person("Alice", 25), Python does this behind the scenes:
Creates a new empty Person object in memory.
Calls __init__(p1, "Alice", 25) to initialize it.
Returns the fully constructed object.
What is self?
The self parameter refers to the current instance (object) being created. You must include it as the
first parameter in any instance method, including __init__(). It lets you store data on that specific object.
Example:
class Dog:
def __init__(self, name):
self.name = name # binds the value to the object
dog1 = Dog("Buddy")
dog2 = Dog("Max")
print(dog1.name) # Buddy
print(dog2.name) # Max
Each dog keeps its own name value because of self.
Default and Optional Parameters
You can give parameters default values, just like with normal functions.
class Car:
def __init__(self, brand, color="white"):
self.brand = brand
self.color = color
c1 = Car("Toyota", "red")
c2 = Car("Honda") # uses default color
print(c1.color) # red
print(c2.color) # white
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Basic Example
When you call
Person("Alice", 25), Python does this behind the scenes:Personobject in memory.__init__(p1, "Alice", 25)to initialize it.What is
self?Example:
Each dog keeps its own
namevalue because ofself.Default and Optional Parameters
You can give parameters default values, just like with normal functions.
__init__()vs Normal Methods__init__()None