Python Constructors

Python Constructors:

Python facilitates a special type of method, also called as Python Constructors, to initialize the instance members of the class and to verify enough object resources for executing any startup task.

Types of Constructors:

  • Parameterized Constructor
  • Non- Parameterized Constructor

Features of Python Constructors: 

  • In Python, a Constructor begins with double underscore (_) and is always named as __init__().
  • In python Constructors, arguments can also be passed.
  • In Python, every class must necessarily have a Constructor.
  • If there is a Python class without a Constructor, a default Constructor is automatically created without any arguments and parameters.

Example:

class Employees(): 
   def __init__(self, Name, Salary): 
       self.Name = Name
       self.Salary = Salary
 
   def details(self): 
       print "Employee Name : ", self.Name
       print "Employee Salary: ", self.Salary
       print "\n"
 
first = Employees("Khush", 10000) 
second = Employees("Raam", 20000)
third = Employees("Lav", 10000)
fourth = Employees("Sita", 30000)
fifth = Employees("Lucky", 50000)
 
first.details() 
second.details() 
third.details()
fourth.details()
fifth.details()

Output:

 

Please follow and like us:
Content Protection by DMCA.com