Back to Python
2026-01-305 min read

Python __init__ Method

Learn Python __init__ Method step by step with clear examples and exercises.

Why This Matters

In Python, the __init__ method plays a crucial role in initializing objects when they are created. It allows you to set default values or perform specific actions during object creation, making it essential for building robust and flexible classes. Understanding how to use the __init__ method can help you avoid common errors and write more efficient code.

The Importance of Initialization

The __init__ method enables us to set up the state of an object at the time of its creation, ensuring that objects are consistent and predictable. Proper initialization helps maintain code readability and reduces the likelihood of errors.

Prerequisites

Before diving into the __init__ method, you should have a good understanding of:

  1. Basic Python syntax and data structures (variables, functions, lists, etc.)
  2. Object-oriented programming concepts (classes, objects, inheritance, etc.)
  3. Understanding how to create classes in Python
  4. Familiarity with instance variables and their usage
  5. Knowledge of Python's scoping rules for variables
  6. Basic understanding of the difference between instance methods, class methods, and static methods

Core Concept

Defining the __init__ Method

To define an __init__ method in a class, simply create a function with the same name as the class and no return type. Inside this function, you can perform any initialization tasks that are specific to your class:

class MyClass:
def __init__(self, param1, param2):
self.param1 = param1 # Assigning instance variables
self.param2 = param2

In the example above, we've created a class called MyClass with two parameters (param1 and param2) that get passed when an object is instantiated:

my_obj = MyClass(5, "Hello")
print(my_obj.param1) # Output: 5
print(my_obj.param2) # Output: Hello

Understanding self

The self keyword in Python refers to the current instance of the class. It is used to access and modify instance variables within the method. In the example above, we use self to assign values to the instance variables param1 and param2.

Calling Parent Class __init__ Methods (Inheritance)

When you inherit from a parent class, you can call its __init__ method using the super().__init__() function. This allows you to reuse initialization logic without duplicating code:

class ParentClass:
def __init__(self, param1):
self.param1 = param1

class ChildClass(ParentClass):
def __init__(self, param1, param2):
super().__init__(param1) # Calling the parent's `__init__` method with param1
self.param2 = param2

In this example, we've created a child class called ChildClass that inherits from ParentClass. When an object of ChildClass is instantiated, it first calls the parent class's __init__ method with the provided parameter (param1) and then assigns its own instance variable (param2).

Instance Variables vs. Class Variables

Note that that instance variables are specific to each object, while class variables are shared among all objects of a given class. To create a class variable, simply assign a value directly to the attribute within the class definition:

class MyClass:
num_instances = 0

def __init__(self):
self.id = MyClass.num_instances
MyClass.num_instances += 1

In this example, we've created a class variable num_instances that keeps track of the number of instances created for the class. Each time an object is instantiated, its instance ID gets assigned based on the current value of num_instances.

Using self to Access Instance Variables

When working with instance variables within methods, always use self to access them:

class MyClass:
def __init__(self, param1):
self.param1 = param1

def display_param(self):
print(self.param1)

my_obj = MyClass("Hello")
my_obj.display_param() # Output: Hello

In the example above, we've created a MyClass instance with an instance variable param1. We then define a method called display_param that accesses and prints the value of param1 using self.

Common Mistakes

  1. Forgetting to call super().__init__() in inherited classes: If you don't call the parent class's __init__ method when inheriting, you may encounter errors due to missing initialization logic.
  1. Using self incorrectly: Make sure to use self within the __init__ method to assign values to instance variables and access them later in your class methods.
  1. Not passing required parameters when instantiating objects: If you don't pass all required parameters when creating an object, it may lead to undefined or incorrect behavior.
  1. Confusing instance variables with class variables: Instance variables are specific to each object, while class variables are shared among all objects of a given class. Be mindful of this distinction when defining and accessing variables within your classes.
  1. Not understanding the difference between instance methods, class methods, and static methods: Instance methods receive an implicit self argument that refers to the current instance, while class methods and static methods don't have access to any instance or class attributes. Use them appropriately based on your needs.
  1. Misusing self in class methods and static methods: Remember that class methods still receive an implicit cls argument, while static methods do not have access to any instance or class variables.

Worked Example

Let's create a simple class called Person with instance variables for name (name) and age (age). Define the __init__ method to accept these parameters and set their values:

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

Instantiate a new Person object with the name "John" and age 30

john = Person("John", 30)


Now that we have our `Person` class set up, let's create an instance method called `introduce()` that prints a personal introduction for each person:

class Person:

def __init__(self, name, age):

self.name = name

self.age = age

def introduce(self):

print(f"Hello! I am {self.name}, and I am {self.age} years old.")

Instantiate a new Person object with the name "John" and age 30, then call the introduce() method

john = Person("John", 30)

john.introduce() # Output: Hello! I am John, and I am 30 years old.

Practice Questions

  1. Create a simple class called Rectangle with instance variables for width (w) and height (h). Define the __init__ method to accept these parameters and set their values.
  1. Write an instance method called area() that calculates and returns the area of the rectangle using its instance variables.
  1. Create a child class called Square that inherits from Rectangle. Override the __init__ method to accept only one parameter (side length) and set both width and height equal to this value.
  1. Write an instance method called perimeter() for the Square class that calculates and returns its perimeter using its instance variables.
  1. Create a new instance of the Square class with a side length of 5, and print its area and perimeter.

FAQ

What happens if I don't define an __init__ method for my class?

If you don't define an __init__ method for your class, Python will create a default constructor that does nothing. However, it is generally recommended to define an __init__ method so that you can initialize instance variables and perform any necessary actions during object creation.

Can I have multiple __init__ methods in the same class?

No, you cannot have multiple __init__ methods with different parameter lists in the same class. Python will only call the first __init__ method it finds when an object is instantiated. If you need to handle different scenarios for object creation, consider using overloading or creating separate classes for each scenario.

Python __init__ Method | Python | XQA Learn