Default constructor in python

Previous
Next

Default constructor:

  • A constructor is added to the python code automatically is called “Default Constructor”.
  • When we don’t define any constructor, a default constructor will be added to source code.
  • Default constructor has Empty Body.
class Test :
    def main(): 
        obj = Test() #invokes default constructor
        print("In main :", id(obj))
        return

Test.main()

We can define the constructor explicitly to check the execution process of constructor in object creation process.

class Test :
    def __init__(self):
        print("Constructor")
        return

    def main(): #static area
        print("main")
        Test() #calling constructor
        Test()
        Test()
        return

Test.main()
Previous
Next

Add Comment

Courses Enquiry Form