Blog Details

img
Data Science

what are Constructors in python

Spoke Right / 14 Nov, 2023

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()

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0