__init__ method in python

__init__ is a method which is defined in a class. Whenever an object of a class is created, this method is called first and arguments are passed. The attributes of a class are initialized using the __init__ method. Below is an example initialization of __init__

class student:
    def __init__ (self,name):
        self.name = name

student_1 = student("TalentEve")
print(student_1.name)

In the above code, we can see that __init__ is the method in class student. keyword self is used  to access the methods and attributes inside the class. It helps bind the attributes with the arguments passed from object.

student_1 is an object of class student and there can be several objects of a class. 
        

Post a Comment

0 Comments