The __init__ method, also known as the constructor method, is a special method in Python classes that gets called automatically when a new instance of the class is created. The __init__ method is used to initialize the attributes of the class and set them to the default values.
This is a Python program defining a class named "user". The class has a constructor method __init__ that takes a name parameter and sets it as an instance variable "self.name". The class also has a method printall that prints the instance variable "self.name".
Two instances of the class, o1 and o2, are created with the names "Tutor Joes" and "Joes" respectively. The printall method is called on each instance to print its name, and the __dict__ attribute is printed to show the instance variables and their values.
Finally, the __dict__ attribute of the class itself is printed to show the class variables and their values, if any.
# init method in Python class user: def __init__(self, name): print("Call When new Instance Created") self.name = name def printall(self): print("Name : ", self.name) o1 = user("Tutor Joes") o1.printall() print(o1.__dict__) o2 = user("Joes") o2.printall() print(o2.__dict__) print(user.__dict__)To download raw file Click Here
Call When new Instance Created Name : Tutor Joes {'name': 'Tutor Joes'} Call When new Instance Created Name : Joes {'name': 'Joes'} {'__module__': '__main__', '__init__': <function user.__init__ at 0x000002485E95B5E0>, 'printall': <function user.printall at 0x000002485E95B670>, '__dict__': <attribute '__dict__' of 'user' objects>, '__weakref__': <attribute '__weakref__' of 'user' objects> '__doc__': None}
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions