When you create an object from a class, you often want to set up its initial state immediately. For instance, if you have a Dog
class, you probably want to specify the dog's name and breed right when you create a Dog
object, rather than creating a generic dog and setting its attributes afterwards.
Python provides a special method for this purpose: __init__
. This method is automatically called whenever you create a new instance (object) of a class. Because it initializes the object's state, it's often referred to as the class constructor, although technically it initializes an already created object. The name __init__
has double underscores before and after it, indicating it's a special method recognized by Python.
__init__
You define __init__
like any other method inside your class definition. The first parameter must always be self
, which refers to the specific instance of the class being created. Following self
, you can define additional parameters to accept data needed to initialize the object.
Inside the __init__
method, you typically assign the values passed through these parameters to the object's attributes using the self.attribute_name = value
syntax.
Let's refine our Dog
class example to include an __init__
method:
class Dog:
def __init__(self, name, breed):
# This method is called automatically when a Dog object is created
print(f"Creating a new Dog object named {name}...")
# Assign the provided name and breed to attributes of the 'self' object
self.name = name
self.breed = breed
self.tricks = [] # Initialize an empty list for tricks
def add_trick(self, trick):
self.tricks.append(trick)
print(f"{self.name} learned a new trick: {trick}!")
# Now, when we create Dog objects, we MUST provide the name and breed
dog1 = Dog("Buddy", "Golden Retriever")
dog2 = Dog("Lucy", "Beagle")
# The attributes were set during creation by __init__
print(f"{dog1.name} is a {dog1.breed}.") # Output: Buddy is a Golden Retriever.
print(f"{dog2.name} is a {dog2.breed}.") # Output: Lucy is a Beagle.
# We can still call other methods
dog1.add_trick("fetch")
print(f"{dog1.name}'s tricks: {dog1.tricks}") # Output: Buddy's tricks: ['fetch']
dog1 = Dog("Buddy", "Golden Retriever")
, Python first creates a new, empty Dog
object.__init__
Call: Python then automatically calls the __init__
method on this newly created object.self
), and any arguments you provided in the parentheses ("Buddy"
, "Golden Retriever"
) are passed as the subsequent arguments (name
, breed
).__init__
, the code self.name = name
assigns the value "Buddy"
to the name
attribute of the dog1
object. Similarly, self.breed = breed
assigns "Golden Retriever"
to the breed
attribute. The self.tricks = []
line initializes the tricks
attribute as an empty list for this specific dog instance.__init__
method doesn't explicitly return
a value (it implicitly returns None
). Its job is to modify the self
object. The instantiation process (Dog(...)
) returns the fully initialized object, which is then assigned to the variable dog1
.Using __init__
is the standard way to ensure that your objects are created with a valid and defined initial state. It makes your classes easier to use correctly because it clearly specifies what information is needed upon object creation. You can also set default values for attributes directly within __init__
, as we did with self.tricks = []
.
© 2025 ApX Machine Learning