__init__ method():-
It is known as a constructor in the object-oriented concepts. This method is called when an object is created from the class and it allows the class to initialize the attributes of a class.
self:-
self is used to represent the instance of the class. With using the “self” keyword we can access the attributes and methods of the class in python.
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
Here the __init__ method gets called when memory for the object is allocated:
x = Point(1,2)
If you want to persist the value with the object, it is important to use the self parameter inside an object's method. You implement the __init__ method like this for instance :
class Point:
def __init__(self, x, y):
self._x = x
self._y = y
To know more about this you can have a look at the following video tutorial:-
Be a Python Expert. Enroll in Python Programming Course by Intellipaat