Constructor in Python-

  • – This is a special method executes every time whenever we create object.
  • – Constructor stores some important information of instance variable.
  • – Inside constructor we used the keyword ‘Self’.
  • – Self variable holds the address of object.
  • – self is a local variable.
  • – We can also display the address of an object by using the pre-defined function named as id() like – id(self)

Ex-

class Test:
    def __init__(self): #Constructor
        print("Constuctor called")
        print(self) #Raw form of address
        print(id(self)) #int form of address

    def main():
        Test()  # Creating object
        y = Test()  # Creating object
        print("y-", y)
        print("y in form of id-", id(y))

Test.main()	
Output-
	Constuctor called
	<__main__.Test object at 0x0000026DC22149E8>
	2670431652328
	Constuctor called
	<__main__.Test object at 0x0000026DC22149E8>
	2670431652328
	y- <__main__.Test object at 0x0000026DC22149E8>
	y in form of id- 2670431652328
Menu