Constructors for classes





Hello folks....
Today let us look at the constructors for classes in python.

Constructors

Constructors are used to instantiate some value to the attributes of the classes associated with the objects. The constructors are invoked automatically when the object is created. 

How to define constructors  in Python?

Constructors are defined using __init__() functions. init keyword is preceded and succeeded by two underscores without space (__). All the instructions that should be executed whenever a new instance is created with the class are defined in the init().

We need not call this function separately using the object name. It is invoked automatically. 

It is a good practice to initialize the class attributes with a constructor.

Types of constructors

  1. Non-parameterized constructors
  2. Parameterized constructors

Non-parameterized constructors

The non-parameterized constructors takes only the object that is being created as the parameter. The values to be initialized for the attributes are set by default.

The 'self ' is the only parameter passed to a non-parameterized constructor. It denotes that all the below instructions must be carried out on the attributes of this corresponding objects.

Let us look at an example. Consider the following code :

class student:
    def __init__(self):
        self.name=""
        self.m1=0
        self.m2=0
        self.m3=0
        self.total=0
    
    def add_details(self,n,a,b,c):
        self.name=n
        self.m1=a
        self.m2=b
        self.m3=c
    
    def calculate(self):
        self.total=self.m1+self.m2+self.m3
        
    def display(self):
        print("name :",self.name)
        print("m1 :",self.m1)
        print("m2 :",self.m2)
        print("m3 :",self.m3)
        print("m4 :",self.total)
        print("\n")
        
s1=student()
s1.add_details("Lizy",56,76,89)
s1.calculate()
s1.display()

s2=student()
s2.add_details("Kate",96,89,99)
s2.calculate()
s2.display()

      
The output for this code is :


Parameterized constructors

The parameterized constructors can be used to initialize the attributes associated with the current object with the user inputs or values passed by the user. Thus apart from 'self ', a list arguments as required must be supplied.

In the above example, the add_details() function can be dropped if parameterized constructors are used. Look at the code below:

class student:
    def __init__(self,n,a,b,c):
        self.name=n
        self.m1=a
        self.m2=b
        self.m3=c
        self.total=0
      
    def calculate(self):
        self.total=self.m1+self.m2+self.m3
        
    def display(self):
        print("name :",self.name)
        print("m1 :",self.m1)
        print("m2 :",self.m2)
        print("m3 :",self.m3)
        print("m4 :",self.total)
        print("\n")
        
s1=student("Lizy",56,76,89)
s1.calculate()
s1.display()

s2=student("Kate",96,89,99)
s2.calculate()
s2.display()
    
The same output will be displayed for this also. For parameterized constructors, one must pass the values to be initialized while creating the objects itself. Those values are assigned to the attributes by init().


Next Topic👉

Comments