Does this code create a circular reference ?

Greg Ewing greg.ewing at compaq.com
Thu Dec 2 05:34:36 EST 1999


Ionel Simionescu wrote:
> 
> class child:
>   def __init__(self, parent):
>     self.parent = parent

>   def do_something(self):
>     pass

> class headache:
>   def method(self):
>     a_child = child(self)
>     a_child.do_something()

As long as a_child.do_something() doesn't cause a reference to
a_child to be stored in its parent (or grand-parent, etc.), there 
is no problem. When headache.method() returns, its local references
will disappear, and the child will be deallocated. No aspirin
required.

Greg




More information about the Python-list mailing list