What is init in Python
__init__ method in Python is similar to constructors in Java, C++.
In Object-Oriented Programming, Constructors are used to initialize the object’s state. i.e. to assign values to the data members of the class when the object of the class is created. It allows the class to initialize the attributes of the class
#a class is created class Student: #init method def __init__(self, name): self.name = name def details(self): print("The name of the student is ", self.name) s = Student('Karan') s.details()
The name of the student is Karan
In this example, a student name Karan is created. While creating a Student(class), “Karan” is passed as an argument, this argument will be passed to the
__init__ method to initialize the object. The keyword
self represents the instance of a class and binds the attributes with the given arguments.
Please login to comment
View all comments