Constructor in Python

 

Python Constructor

A constructor is a special method used to initialize instance variable
  • Name of constructor must be _ _ init _ _()

#program to define a constructor

class Employee_data :
def __init__(self):
print("This is a Python Constructor")


  • It is called at object creation time.

#program to define a constructor

class Employee_data :
def __init__(self):
print("This is a Python Constructor, called at Object creation Time")

Emp_data = Employee_data()

 #Output

This is a Python Constructor, called at Object creation Time


  • Constructor is called once for every object.

#program to define a constructor

class Employee_data :
def __init__(self):
print("This is a Python Constructor, called at Object creation Time")

Emp_data_1 = Employee_data()
Emp_data_2 = Employee_data()

 #Output

This is a Python Constructor, called at Object creation Time
This
is a Python Constructor, called at Object creation Time


  • Constructor takes "self" as a default parameter.

#program to define a constructor

class Employee_data :
def __init__(self):
self.a = 10
self.b = 5
print("This is a Python Constructor, called at Object creation Time")

def display(self):
print("a :", self.a)
print("b :", self.b)

Emp_data = Employee_data()
Emp_data.display()

 #Output

This is a Python Constructor, called at Object creation Time
a :
10
b : 5


  • Constructors are of 2 type
    • Default Constructor (Non-Parameterized)
                        It holds common value for every object 

    #program to define a constructor

    class Employee_data :
    def __init__(self):
    self.name = "Techno Xpresss"
    self.website = "www.salahtutorials.com"
    print("This is a Python Constructor, called at Object creation Time")

    def display(self):
    print("Name :", self.name)
    print("website :", self.website)

    Emp_data_1 = Employee_data()
    Emp_data_2 = Employee_data()
    Emp_data_1.display()
    Emp_data_2.display()

     #Output

    This is a Python Constructor, called at Object creation Time
    This
    is a Python Constructor, called at Object creation Time
    Name : Techno Xpresss
    website : www.salahtutorials.com
    Name : Techno Xpresss
    website : www.salahtutorials.com


      • Parameterized Constructor
                        It holds unique value for every object

    #program to define a constructor

    class Employee_data :
    def __init__(self, name): # constructor accepts value as parameter
    self.name = name
    self.website = "www.salahtutorials.com"
    print("This is a Python Constructor, called at Object creation Time")

    def display(self):
    print("Name :", self.name)
    print("website :", self.website)

    Emp_data_1 = Employee_data(
    "Techno Xpresss") # passing value to constructor while object creator
    Emp_data_2 = Employee_data("Er M S Dandyan") # passing value to constructor while object creator
    Emp_data_1.display()
    Emp_data_2.display()

     #Output

    This is a Python Constructor, called at Object creation Time
    This
    is a Python Constructor, called at Object creation Time
    Name : Techno Xpresss
    website : www.salahtutorials.com
    Name : Er M S Dandyan
    website : www.salahtutorials.com





    Comments

    My photo
    Techno Xpresss
    Bangalore, Karnataka, India