The __init__ Function of class in python



The __init__ function is a special type of function used in class ,which is used to create a function , for which user does not create an calling function , instead the object which you have created will be used to call the function i.e the __init__ function will be automatically called whenever an object is created


Example:

class student:  # creating class named as student
   
        def __init__(self,name,rollno):  # defining function inside of class

                self.name = name            
                self.rollno = rollno

s1 = student(" kangroo", 34)#object of class student in which values are passed
s2 =  student("chrintie" , 15)

print( s1.name, s1.rollno)     # printing  object s1 arguments
print(s2.name, s2.rollno)       # printing object s2 arguments



as we can see from above that we didn't create any calling function .




Output


Thankyou ;
and Please  Leave Your Remark

Comments