__ init __ in Python

If you have been using object-oriented programming, you may have run into the word "init" quite often. __init__ is a Python method. It is analogous to the constructors in languages like Java and C++. Knowing classes and objects in Python will make the __init__ method understandable.

Some Rpre-requisite Knowledge:

  • A class is a blueprint with declared variables, attributes, and methods. We must make objects for the newly generated class to use it.
  • We can access the declared attributes and call the class's methods using the objects.
  • Each object can have its values for the class's attributes. When creating the object, we can pass the desired values as arguments.

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

Output

Planet
Solar  system
I'm earth
I'm a planet in Solar system

Analysis

A class called Planet was made. During class:

  • Two variables, var1, and var2, were declared.
  • We made a function, and it printed two strings using the class's stated variables.

After creating the object Earth, we used the class's two variables and method without supplying any inputs.

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

Now, what is self in the class?

The self is substituted with the newly produced object when the function is called after creating an object for the class. It functions as the object's kind of placeholder. All the objects we generate share two variables in the class we constructed. Therefore, even if we called the variables by the names of the objects, we would still receive the same results.

Let's now examine what '__init__' is capable of.

  • As was said above, each object may have its values for a class's attributes. The __init__ method can implement this functionality.
  • It is a constructor that enables classes to contain objects with various values.
  • It is not necessary to call it a typical approach. It is comparable to a class method. It is put into action as soon as an object is produced for the class.

Let's look at the example from above using the __init__ method now:

Output

I am earth
I am the 3 planet in the solar system

Understanding:

  • When we built the object, we supplied arguments to it.
  • As was previously mentioned, when an object is made:
  • The __init__ method is run.
  • The newly formed object is used in place of 'self.'

When the object "earth" was created:

This is the __init__ method's internal workings.

The planet Earth () can have its characteristics in this fashion.

If we make another item now:

Output

I am earth
I am the 3 planet in the solar system
I am venus
I am the 2 planet in the solar system

As a result, we can build an unlimited number of objects, each with a unique set of attribute values.

The __init__ method in Python's object-oriented programming has these capabilities.

Let's look at one more instance:

  • We can accept the user's input as values and then pass those values as attributes to an object.

Output

Please enter the name of the student1: Jeevani
Please enter the age of the student1: 19
Please enter the name of the student2: Harini
Please enter the age of the student2: 15
Stud_1.name = Jeevani
Stud_2.name = Harini

Understanding:

We established a class called "Student" with the following three properties in the program: name, age, and email. We defined the attributes using the self-variable in the __init__ method. Two objects?stud and stud2?were produced. We already provided the email attribute value for both objects and requested the

user's input for the name and age attributes before passing the values to the objects.

Note:

  • Any number of objects, properties, and functions are possible inside a class. However, a class can only contain one explicit __init__ method.
  • Even if we write numerous __init__ methods, the most recent one will always replace the older ones.





Latest Courses