VOOZH about

URL: https://www.geeksforgeeks.org/python/__init__-in-python/

⇱ __init__ in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

__init__ in Python

Last Updated : 11 May, 2026

__init__() method is a constructor which is automatically called when a new object of a class is created. Its main purpose is to initialize the object’s attributes and set up its initial state. When an object is created, memory is allocated for it and __init__ helps organize that memory by assigning values to attributes.

Let’s look at some examples.

__init__() with Parameters

You can pass parameters to initialize multiple attributes.


Output
Max 30
Emilia 25

Explanation:

  • self: refers to the current object (always the first parameter).
  • name and age: parameters passed when creating the object.
  • self.name and self.age: object attributes that store these values.

Default Parameters in __init__()

Like regular functions, __init__() can have parameters with default values.


Output
Buddy Mixed 1
Max Golden Retriever 5

Explanation:

  • breed="Mixed" and age=1 are default values.
  • When creating Dog("Buddy"), Python uses the defaults, breed = "Mixed", age = 1.
  • When creating Dog ("Max", "Golden Retriever", 5), the defaults are overwritten by the provided values.

__init__() Method with Inheritance

When using inheritance, both parent and child classes can have __init__() methods.


Output
A init called
B init called

Explanation:

  • When obj = B() is created, Python first calls B.__init__(). Inside B.__init__, the line super().__init__() calls the parent’s (A) constructor.
  • As a result, "A init called" is printed first.
  • After the parent class initialization completes, remaining code in B.__init__() executes and "B init called" is printed.

Related Articles:

Comment
Article Tags:
Article Tags: