-1

here is my code

class third:

    def __init__(self):
        print("Cons is called")

    def __del__(self):
        print("desc is called")

    def setName(self,firstName,lastName):
        self.firstName=firstName;
        self.lastName=lastName;

    def displayName(self):
        print(self.firstName,' ' ,self.lastName)

ob=third(); 
ob.setName('gaurav','sharma');
ob.displayName()
ob.__del__()
ob.displayName();

and output is

  Cons is called
  gaurav   sharma  
  desc is called
  gaurav   sharma
  desc is called

i have destroyed object by using del and then tried to call displayname function again still it is providing distroyed values and now i m confused weather the object is destroid or not.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
Tiger
  • 136
  • 9

1 Answers1

1

You should not call __del__() directly as it will only run that method but won't actually delete the object. Use the del statement to delete an object:

>>> del ob
>>> ob
>>> NameError: name 'ob' is not defined
Selcuk
  • 57,004
  • 12
  • 102
  • 110