what is a constructor in python?

0 votes
can you give an example?
Mar 29 in Python by Waseem
• 1,870 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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();  
ID: 101 
Name: John
ID: 102 
Name: David
answered Apr 1 by Tushar
• 200 points

Related Questions In Python

0 votes
2 answers

What do you mean by python scripting? What is a script and a module in python?

A scripting language is a programming language ...READ MORE

answered Mar 14 in Python by rajesh kumar
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,100 points
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
• 9,380 points
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,710 points
0 votes
2 answers

What is a Tuple in Python and how to use it?

  Tuple is a collection of Python objects.it is ...READ MORE

answered Apr 12 in Python by rajesh
• 1,210 points
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,100 points
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 920 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.