Table of Contents
- Introduction of __init__ in Python
- Key Points about __init__ in Python:
- Conclusion of __init__ in Python:
Introduction of __init__ in Python
We often refer to the special method __init__ in Python as a constructor. Moreover, we automatically call the special method __init__ when we create a new instance of a class.
We use it to initialize the object’s attributes and perform any setup operations needed for the instance.
The __init__
method allows you to define the initial state of an object which is an essential part of object-oriented programming in Python.
Key Points about __init__
in Python:
- Initialization of Object State:
- The
__init__
method allows you to set the initial values of the attributes of an object when it is created. - We commonly use it to initialize the instance variables with default or provided values.
- The
- It’s Not a Constructor:
- While
__init__
is often called the constructor, technically in Python, it is not the constructor. The actual constructor is__new__
, which creates the object in memory. The__init__
method is responsible for initializing the object after it has been created.
- While
- Can call Automatically:
- When you create a new object of a class, Python automatically calls the
__init__
method to initialize that object So, you do not need to call it explicitly.
- When you create a new object of a class, Python automatically calls the
- Self Parameter:
- We always use
self
as the first parameter of the__init__
method. Which also refers to the instance of the class which we create and allow the method to modify the instance’s attributes. - You can think of
self
as a reference to the object itself.
- We always use
- Arguments:
- You can define the
__init__
method with parameters so that you can pass values when creating the object. We can use these values to initialize the object’s state(Attributes).
- You can define the
- Optional Arguments:
- We can provide default values for the parameters which calls the methods with fewer arguments.
Syntax of __init__
:
class MyClass:
def __init__(self, param1, param2):
# Initialize instance variables
self.attribute1 = param1
self.attribute2 = param2
Example with Default Arguments:
class Car:
def __init__(self, make="Unknown", model="Unknown", year=2020):
self.make = make
self.model = model
self.year = year
# Creating objects with different numbers of arguments
car1 = Car() # Uses default values
car2 = Car("Toyota", "Camry") # Defaults year to 2020
car3 = Car("Ford", "Mustang", 2022)
print(car1.make, car1.model, car1.year) # Output: Unknown Unknown 2020
print(car2.make, car2.model, car2.year) # Output: Toyota Camry 2020
print(car3.make, car3.model, car3.year) # Output: Ford Mustang 2022
Conclusion of __init__ in Python:
We use the init method to initialize an object’s state when we create it. This allows us to set up instance variables, run setup code, and configure the object according to the arguments we pass to it.
As such, we often refer to it as the constructor but technically, it is the initializer in Python.
Leave a Reply