Python Tutorial-2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Python Constructor

A constructor is a special type of method (function) which is used to initialize the


instance members of the class.

Constructors can be of two types.

1. Parameterized Constructor
2. Non-parameterized Constructor

Constructor definition is executed when we create the object of this class. Constructors
also verify that there are enough resources for the object to perform any start-up task.

Creating the constructor in python


In python, the method __init__ simulates the constructor of the class. This method is
called when the class is instantiated. We can pass any number of arguments at the time
of creating the class object, depending upon __init__ definition. It is mostly used to
initialize the class attributes. Every class must have a constructor, even if it simply relies
on the default constructor.

Consider the following example to initialize the Employee class attributes.

Example
class Employee:  
    def __init__(self,name,id):  
        self.id = id;  
        self.name = name;  
    def display (self):  
        print("ID: %d \nName: %s"%(self.id,self.name))  
emp1 = Employee("John",101)  
emp2 = Employee("David",102)  
  
#accessing display() method to print employee 1 information  
   
emp1.display();   
  
#accessing display() method to print employee 2 information  
emp2.display();   

Python Non-Parameterized Constructor Example


class Student:    
    # Constructor - non parameterized    
    def __init__(self):    
        print("This is non parametrized constructor")    
    def show(self,name):    
        print("Hello",name)    
student = Student()    
student.show("John")

    Python Parameterized Constructor Example


class Student:    
    # Constructor - parameterized    
    def __init__(self, name):    
        print("This is parametrized constructor")    
        self.name = name    
    def show(self):    
        print("Hello",self.name)    
student = Student("John")    
student.show()  

Python In-built class functions


The in-built functions defined in the class are described in the following table.

SN Function Description

1 getattr(obj,name,default) It is used to access the attribute of the object.

2 setattr(obj, name,value) It is used to set a particular value to the specific attribute

3 delattr(obj, name) It is used to delete a specific attribute.

4 hasattr(obj, name) It returns true if the object contains some specific attribu

Example
class Student:  
    def __init__(self,name,id,age):  
        self.name = name;  
        self.id = id;  
        self.age = age  
  
#creates the object of the class Student  
s = Student("John",101,22)  
  
#prints the attribute name of the object s  
print(getattr(s,'name'))  
  
# reset the value of attribute age to 23  
setattr(s,"age",23)  
  
# prints the modified value of age  
print(getattr(s,'age'))  
  
# prints true if the student contains the attribute with name id  
  
print(hasattr(s,'id'))  
# deletes the attribute age  
delattr(s,'age')  
  
# this will give an error since the attribute age has been deleted  
print(s.age)  

You might also like