oop - Why I cannot delete a class instance in python -


i want delete class instance or make none in python. i'm not able it. in general code follows:-

class foo:   pass  f = foo() g = foo() f.attr = g h = f.attr del h print f.attr  # not none should right? 

more problem came while implementing binary search trees in python. delete function this:-

class tree:   def delete(self, key):     min_right = self.right.getmin()     self.key = min_right.key     del min_right  # not work 

am doing wrong or expected behavior? if expected, best way oop standards achieve trying do?

you have situation looks this:

                  g                    |       <__main__.foo object @ 0x...>            /               \         f.attr               h 

i.e. 3 references same underlying object. del h remove name h, this:

                  g                    |       <__main__.foo object @ 0x...>            /                        f.attr           

the underlying object doesn't change, , 2 other names still provide reference it, you've done remove 1 reference foo instance. h nameerror, g , f.attr work fine.

 # not none should right? 

i don't know why conclude - if did succeed in breaking reference f.attr foo instance, attributeerror, not none.

recommended reading on python names: http://nedbatchelder.com/text/names.html


Comments