Name and explain the three magic methods of Python that are used in the construction and initialization of custom Objects?

  • The 3 magic methods of Python that are used in the construction and initialization of custom Objects are:
    •  init__, 
    • new , and 
    • del__.
  • new – 
    • this method can be considered as a “constructor”. 
    • It is invoked to create an instance of a class with the statement say, 
      • myObj = MyClass()
  • init__ — 
    • It is an “initializer”/ “constructor” method. 
    • It is invoked whenever any arguments are passed at the time of creating an object. 
      • myObj = MyClass(‘Pizza’,25)
  • del- 
    • this method is a “destructor” of the class. 
    • Whenever an object is deleted,invocation of del__ takes place and it defines behaviour during the garbage collection. 
      • Note: new , del are rarely used explicitly.

No comments:

Post a Comment