English

How will you create a constructor in Python?

Advertisements

Question

How will you create a constructor in Python?

Short/Brief Note
Advertisements

Solution

  • In Python, there is a special function called “init” which acts as a Constructor.
  • It must begin and end with a double underscore.
  • This function will act as an ordinary function; but the only difference is, it is executed automatically when the object is created.
  • This constructor function can be defined with or without arguments.
  • This method is used to initialize the class variables.

The general format of _init_method (Constructor function):
def _init_(self, [args………… ]):
< statements >

shaalaa.com
Constructor and Destructor in Python
  Is there an error in this question or solution?
Chapter 10: Python Classes and objects - Evaluation [Page 187]
Advertisements
Share
Notifications



      Forgot password?
Use app×