what is a constructor in python?

0 votes
can you give an example?
Mar 29 in Python by Waseem
• 4,480 points
32 views

1 answer to this question.

0 votes

Constructors are for instantiating an object.The task of constructors is to initialize the data members of the class when an object of class is created. The __init__() method is called the constructor in Python and is always called when an object is created.

class Employee:  

  1.     def __init__(self,name,id):  
  2.         self.id = id;  
  3.         self.name = name;  
  4.     def display (self):  
  5.         print("ID: %d \nName: %s"%(self.id,self.name))  
  6. emp1 = Employee("John",101)  
  7. emp2 = Employee("David",102)       
  8. emp1.display();   
  9. emp2.display();  
Output:
ID: 101 
Name: John
ID: 102 
Name: David
answered Apr 1 by Tushar
• 200 points

Related Questions In Python

+1 vote
3 answers
0 votes
1 answer

What is the meaning of “int(a[::-1])” in Python?

Assumming a is a string. The Slice ...READ MORE

answered Aug 27, 2018 in Python by Priyaj
• 56,520 points
70 views
0 votes
1 answer

What is a “method” in Python?

It's a function which is a member ...READ MORE

answered Oct 23, 2018 in Python by SDeb
• 13,160 points
26 views
0 votes
1 answer

What is the process to kill a particular thread in python?

A multiprocessing.Process can p.terminate() In the cases where I want to ...READ MORE

answered Feb 4 in Python by charlie_brown
• 7,720 points
54 views
0 votes
3 answers
0 votes
1 answer

What is a dictionary in Python?

Dictionary can be understood as a key-value ...READ MORE

answered Mar 4 in Python by Priyaj
• 56,520 points
26 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 233 views