Javatpoint Logo
Javatpoint Logo

__init__ in python

If you have been working with the Object-oriented programming, you might have come across _init_ word quite a few times. __init__ is a Python method. It is similar to the constructors in languages like Java and C++. Knowing classes and objects in Python will make the __init__ method understandable.

Here is some required pre-requisite knowledge:

  • A class is like a blueprint with variables/ attributes and functions/ methods declared. To use the class, we need to create objects for the created class.
  • Using the objects, we can call the methods in the class and access the declared attributes.
  • Every object can have its values for the attributes in the class. We can pass the values we want as arguments when creating the object.

Here is a simple example of a class and an object:

Output:

__init__ in python

Analysis:

We created a class named planet. In the class:

  1. We declared two variables, var1 and var2.
  2. We created a function where we printed two strings with the declared variables inside the class.

Now, we created object earth and accessed the two variables and the method from the class without passing any arguments.

  • The object we created doesn't have its variables.

Now, what is self in the class?

When we create an object for the class and call the function, the self is replaced with the created object. It is like a placeholder for the object. In the class we created, we have two variables common to all the objects we create. Hence, even if we called the variables with the object name, we will get the same values for all the objects.

Now, let us understand what '__init__' can do?

  1. We discussed above that every object could have its values for the attributes of a class. This functionality can be achieved using the __init__ method.
  2. It is a constructor, allowing a class to hold objects with different values.
  3. We need not call it like we call a normal method. It is similar to a method inside a class. It is executed as soon as an object is created for the class.

Now, let us see the above example with __init__ method:

Output:

__init__ in python

Understanding:

  1. We passed arguments to the object when we created it.
  2. As we discussed earlier, when an object is created:
  3. __init__ method is executed.
  4. 'self' is replaced with the created object.

When we created the object 'earth':

This is the inner mechanism in the __init__ method.

This way, the object earth () can have its attributes.

Now, if we create another object:


__init__ in python

Hence, we can create any number of objects, and every object can have its values for the attributes.

This is the functionality of the __init__ method in object-oriented programming in Python.

Let us see one more example:

  • We can take the values as inputs from the user and then pass them as attributes to an object.

Output:

__init__ in python

Understanding:

In the program, we created a class called "Student" with three attributes: name, age and email. Using the self variable, we defined the attributes in the __init__ method. We created two objects, stud and stud2. For both the objects, we already gave the value for email and asked the user for inputs for name and age attributes and then passed the values to the objects.

Note:

  • We can create any number of objects and any number of attributes and functions inside a class. But, there can only be one explicit __init__ method inside a class.
  • Even if we write multiple __init__ methods, the latest one will overwrite the previous __init__ methods.

Next Topic__dict__ in Python





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA