हिंदी

How will you create a constructor in Python?

Advertisements

प्रश्न

How will you create a constructor in Python?

टिप्पणी लिखिए
Advertisements

उत्तर

  • 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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 10: Python Classes and objects - Evaluation [पृष्ठ १८७]
Advertisements
Share
Notifications



      Forgot password?
Use app×