0 votes
in Python by
How can we create a constructor in Python programming?

1 Answer

0 votes
by

The _init_ method in Python stimulates the constructor of the class. Creating a constructor in Python can be explained clearly in the below example.

class Student:  

def __init__(self,name,id):  

self.id = id;  

self.name = name;  

def display (self):  

print("ID: %d nName: %s"%(self.id,self.name))  

stu1 =Student("nirvi",105)  

stu2 = Student("tanvi",106)  

#accessing display() method to print employee 1 information     

stu1.display();  

#accessing display() method to print employee 2 information  

stu2.display();

Output:

ID: 1

Name: nirvi

ID: 106

Name: Tanvi

Related questions

0 votes
asked Feb 11, 2020 in Python by rahuljain1
0 votes
asked May 24, 2020 in Python by SakshiSharma
...