python - __del__method not executing when involving circular reference -


i find custom __del__ method seems not execute when circular referencing involved.

here sample code:

class delclass():         def __init__(self,a):             self.prop =           def __del__(self):             print 'del'    if __name__ == "__main__":      dc1 = delclass(none)     dc2 = delclass(dc1)#dc2 referring dc1     dc1.prop = dc2#dc1 referring dc2, creating circular reference     del dc1#not executing custom __del__ method      dc = delclass(1)     del dc#executing custom __del__ method 

why happens?

edit: brenbarn. found reason.

del something decrements reference count of something 1.

__del__ execute when reference count reaches 0.

here test code:

import gc  class delclass():     def __init__(self,name,a):         self.name = name         self.prop =      def __del__(self):         print '#####deleting',self.name  dc1 = delclass("dc1",none) dc2 = delclass("dc2",dc1)#dc2 referring dc1 dc1.prop = dc2#dc1 referring dc2, creating circular reference print "before deleting dc1,reference count:",len(gc.get_referrers(dc1)) del dc1#not executing custom __del__ method print "after deleting dc1, reference count:",len(gc.get_referrers(dc2.prop)) print "deleting reference held dc2" del dc2.prop print dc2 

the output is:

before deleting dc1,reference count: 2 after deleting dc1, reference count: 1 deleting reference held dc2 #####deleting dc1 <__main__.delclass instance @ 0x9316dec> #####deleting dc2 

and question appears:

why last line(#####deleting dc2) in output happens?

some implicit del operation happens?

read documentation __del__ , for garbage collector. __del__ doesn't think does, nor del. __del__ not called when del, , may never called in case of circular references. del decrement reference count 1.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -