Python
In this tutorial we will learn about the class __init__ method in Python.
__init__
We learned about classes and how to create objects using classes in the Python - Classes and Objects tutorial. Feel free to check that out.
The __init__ method is a special method of a class.
It is also called the constructor method and it is called when we create (instantiate) an object of the class.
We use the __init__ method to initialise class attributes or call class methods.
In the following Python program we are creating the __init__ method inside the Awesome class.
Awesome
# class class Awesome: # the init method def __init__(self): print("Hello from the __init__ method.") # object of the class obj = Awesome()
The above code will print the following output.
Hello from the __init__ method.
We get the above output because as soon as we create an object of the class the __init__ method is automatically called.
Points to note!
The first parameter of the __init__ method is always self.
self
The self refers to the object of the class and we use it to access methods and class attributes from inside the class.
Like any other methods of a class we can also pass arguments to the __init__ method.
In the following Python program we are passing a string value to the __init__ method at the time of creating an object.
# class class Awesome: # the init method def __init__(self, name): print("Hello from the __init__ method.") print("Name:", name) # object of the class obj = Awesome("Yusuf Shakeel")
The above code will give us the following output.
Hello from the __init__ method. Name: Yusuf Shakeel
We can call other methods of the class from the __init__ method by using the self keyword.
# class class Awesome: # the init method def __init__(self): print("Hello from the __init__ method.") # calling the class method self.greetings() # methods def greetings(self): print("Hello from the greetings() method.") # object of the class obj = Awesome()
Hello from the __init__ method. Hello from the greetings() method.
We can create class properties that are specific to the class objects and initialise them inside the __init__ method.
In the following Python program we are creating Awesome class and initialising class property name inside the __init__ method.
name
# class class Awesome: # class attribute common to all objects commonAttr = "Common Attribute" # the init method def __init__(self, name): # class attribute for the objects self.name = name # methods def greetings(self): print("Hello %s!" %self.name) # object of the class obj1 = Awesome("Tom") obj2 = Awesome("Jerry") # output print("The common class attribute:") print("obj1.commonAttr:", obj1.commonAttr) print("obj2.commonAttr:", obj2.commonAttr) print("Class attribute specific to object:") print("obj1.greetings():") obj1.greetings() print("obj2.greetings():") obj2.greetings()
The common class attribute: obj1.commonAttr: Common Attribute obj2.commonAttr: Common Attribute Class attribute specific to object: obj1.greetings(): Hello Tom! obj2.greetings(): Hello Jerry!