constructor in python | Types of Constructor in python | default constructor and parameterized constructor in python |




constructor in python | Types of Constructor in python |default constructor and parameterized constructor in python |


Constructor

1) Constructor is a special type of method or function which is use to initialize the instance member of class.
2) Constructor is a special type function that is call automatically whenever an object of that class is created.
3) Constructor is a special member function for automatic initialization of an object.


constructor in python | Types of Constructor in python |
Constructor in Python 






Creating Constructor in Python :-

         Constructor are generally use for initializing an object. The task of constructor is to initialize ( assign value ) to data member of class.
           When an object of class is created in python  __init__ ()  is called constructor and is always called when an object is created.

Syntax of Constructor Declaration –

def  __init__(self):
      #body of the constructor 


Note -

Constructor always has name init and name init is prefix and suffix with double underscore
 ( __ ) . we declare a constructor using def  keyword just like method .



Types of Constructor:-

There are two type of constructor 
1) Default Constructor (non parameterized constructor )
2) Parameterized Constructor


1) Default Constructor :-

           Default Constructor is a simple constructor which does not accept any argument. its definition has only one argument which is a reference to a instance being constructed. (i.e. self)  
Or 
Default constructor is constructor that may be called without parameter .this may be achieve by either providing default argument to constructor . 

Default constructor function initialize data member with no arguments .

Program -

Addition of two no's using default constructor



class add:
def __init__(self):
print("Adition of two numbers is ")
def show(self,x,y):
print(x+y)
ob = add()
ob.show(1,2)


Output -



Addition of two numbers is 
3


Program -

Addition of two no's using parameterized constructor


class add:
def __init__(self,x,y):
self.x=x
self.y=y
def display(self):
print("First Number is ",self.x)
print("Second Number is ",self.y)
print("Addition is ",self.x+self.y)
ob=add(2,3)
ob.display()

Output


First Number is  2
Second Number is  3
Addition is  5



2) Parameterized Constructor -

Constructor with parameter is known as parameterized constructor. The parameterized constructor take first argument as reference to the instance being constructed known as ‘self’ and the rest of argument are provided by programmers. 

Program -

Addition of two no's using default constructor

class add:
def __init__(self,x,y):
self.x=x
self.y=y
def display(self):
print("First Number is ",self.x)
print("Second Number is ",self.y)
print("Addition is ",self.x+self.y)
ob=add(2,3)
ob.display()



Output -


First Number is  2
Second Number is  3
Addition is  5













Post a Comment

Post a Comment (0)

Previous Post Next Post