What is __init__() in python | explain built in function __init__()


What is __init__() in python | explain built in function __init__()





__init__() Method :-



The __init__() method is called automatically every time in the class is being use to create new object.
__init__ is a method /constructor in python . this method is automatically called to allocate memory when a new object /instance of class is created . all classes have the  __init__ method To understand meaning of classes we have to understand built in __init__() function .
All classes have function called  __init__()  which always executed when class being initiated.
 __init __()function use to assign values to object properties.
 and also __init__() function is used when an object is being created.



What is __init__() in python | explain built in function __init__()
Using __init__() function 


Example -

class employee:
    def __init__(self,name,id,salary):
        self.name=name
        self.id=id
        self.salary=salary
emp1=employee("Abdul Kalam ",123,50000)
print("Employee id:",emp1.id)
print("Employee name: ",emp1.name)





Output -

Employee id: 123
Employee name:  Abdul Kalam 











Post a Comment

Post a Comment (0)

Previous Post Next Post