in Python by
Q:
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

Click here to read more about Python
Click here to read more about Insurance

Related questions

0 votes
asked Feb 11, 2020 in Python by rahuljain1
0 votes
asked May 17, 2020 in Python by SakshiSharma
0 votes
asked Dec 19, 2019 in Python by sharadyadav1986
0 votes
asked May 16, 2020 in Python by Robindeniel
0 votes
asked Mar 8, 2020 in Git Slack Integration by Hodge
...