__init__ in Python
### What is `__init__` in Python?
`__init__` is a special method in Python classes, also known as a constructor. It is called when an instance of a class is created, and is used to set up the attributes of the object. It is also used to initialize variables and other data structures used by the class.
The purpose of `__init__` is to provide a way to set up the instance of the class and make it ready for use. This can include initializing all the attributes of the class, setting default values, and more. It is important to remember that the `__init__` method will be called every time an instance of the class is created.
`__init__` is also used to provide access to the parent class. This allows the child class to inherit the data and methods of the parent class. This is done by including the `super()` method in the `__init__` method of the child class.
Aside from setting up the instance, `__init__` can also be used to accept arguments that can be passed to the instance. This can be useful for setting up specific variables or attributes with specific values.
In summary, `__init__` is a special method used in Python classes that is used to initialize the attributes of the class and provide access to the parent class. It is also used to receive arguments that can be used to set up the instance of the class.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
The __init__() method in Python is a special method used to initialize a newly created object. It is the first method called when a new object of a class is instantiated.
__init__() can be used to set the values of instance variables at the time of object initialization. It is also known as a constructor.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def print_info(self):
print(f'Name: {self.name}, Age: {self.age}')
person = Person('John', 25)
person.print_info()
# Output
Name: John, Age: 25