Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - CONSTRUCTORS


'Constructors' or the '__init__( )' method provides us a way to initialise the attributes of the class while an object is created.


The '__init__( )' method looks a little different from the other methods. i.e. It starts with two underscores '__' and ends with two underscores '__' .


Well ! That is because it is not like a normal method and is called a 'Constructor' .


There are two types of Constructor :


  1. Default Constructor or the no argument Constructor(i.e. '__init__( )') .
  2. Constructor with arguments or Parameterised Constructor(i.e. '__init__(arg1, arg2, ...)') .

Default Constructor '__init__( )'


The '__init__( )' method with no arguments is the 'Default Constructor' . It is called so because, even though we don't call the '__init__( )' method, it is automatically called at the time of Object creation.


Let us see in the below example .


Example :



class Human:

    def eat(self):
        print(self.name," eats ",self.food)

    def speak(self):
        print(self.name," speaks ",self.language)
		
    def __init__(self):
        print("__init__() method is called during object creation")


human1 = Human()


Output :



  __init__( ) method is called during object creation

In the above example , we have create the 'Human' class,


class Human:

    def eat(self):
        print(self.name," eats ",self.food)

    def speak(self):
        print(self.name," speaks ",self.language)
		
    def __init__(self):
        print("__init__() method is called during object creation")

With the '__init__( )' method,


def __init__(self):
    print("__init__() method is called during object creation")

java_Collections

Just to demonstrate that you don't have to call the '__init__( )' method separately. But it would be called automatically, at the time of object creation.


human1 = Human( )

So , the moment we create the object using 'Human( )', '__init__( )' method is called automatically and we get the below output .


__init__( ) method is called during object creation

Note : The 'self' keyword is a mandatory argument in all the methods in the class that specifies the method is a part of that class.

Constructor with Arguments '__init__(arg1, arg2, ...)'


Now , let us see, how can we initialise all the attributes of the 'Human' class using the Constructor with Arguments.


Let us take the same 'Human' class from the previous tutorial.


Example :



class Human:

    def __init__(self, name, food, language):
        self.name = name
        self.food = food
        self.language = language

    def eat(self):
        print(self.name," eats ",self.food)

    def speak(self):
        print(self.name," speaks ",self.language)
		

human1 = Human("John", "Burger", "English")
human2 = Human("Rakhi", "Rice", "Hindi")

human1.eat()
human1.speak()

human2.eat()
human2.speak()


Output :



  John eats Burger
  John speaks English
  Rakhi eats Rice
  Rakhi speaks Hindi

Now , the above example has a structured way of defining the attributes of the class( i.e. 'name', 'food' and 'language' ).


All we have done is, while creating the object 'human1' , we have passed the values, "John","Burger" and "English" as Arguments.


human1 = Human("John", "Burger", "English")

And what happens is, the constructor, 'init(...)' with three Arguments is called.


def __init__(self, name, food, language):
    self.name = name
    self.food = food
    self.language = language

java_Collections

And "John" gets initialised to the variable 'name', "Burger" to 'food' and "English" to the variable 'language' .


java_Collections

And 'self' refers to 'human1' object.


But ! Hold on !


The values are not yet a part of the 'human1' Object. They becomes a part of 'human1' Object in the next lines.


self.name = name
self.food = food
self.language = language

As 'self' refers to 'human1' object.


And


self.name = name

Takes the value 'John' from the local variable 'name' and puts it to the 'name' attribute of the 'Human' class.


Similarly , the next two lines ,


self.food = food
self.language = language

Assigns the values 'Burger' and 'English' to the 'food' and 'language' attribute of the 'human1' object.


java_Collections

And the 'human1' object has the values now.


java_Collections

Similarly , the next line ,


human2 = Human("Rakhi", "Rice", "Hindi")

Assigns the values to the 'human2' object.


java_Collections

How to Modify the properties of an Object


Let us take the same example . So , 'John' speaks 'English' .


Now , let's say we want to modify the language of 'John' to 'Spanish' . Let modify the same example.


Example :



class Human:

    def __init__(self, name, food, language):
        self.name = name
        self.food = food
        self.language = language

    def eat(self):
        print(self.name," eats ",self.food)

    def speak(self):
        print(self.name," speaks ",self.language)
		

human1 = Human("John", "Burger", "English")
human2 = Human("Rakhi", "Rice", "Hindi")

human1.language = "Spanish"

human1.eat()
human1.speak()

human2.eat()
human2.speak()


Output :



  John eats Burger
  John speaks Spanish
  Rakhi eats Rice
  Rakhi speaks Hindi

And all we have done is, added the line ,


human1.language = "Spanish"

And the 'language' attribute of 'human1' object got changed to 'Spanish' .


java_Collections

And we got the below output .


John speaks Spanish