circular references?

dj trombley badzen at yifan.net
Sat Dec 18 00:28:33 EST 1999


Roy Smith wrote:
> 
> Let's say I've got:
> 
> class a:
>    pass
> 
> class b:
>    def __init__ (self, x):
>       self.x = x
> 
> and then I do:
> 
> foo = a()
> foo.b = b(foo)
> 
> I've now got two objects that contain references to each other.  Is this
> bad?

Introspection: That all depends on what one views as 'bad'. =)

What it _does_ actually do is prevent the garbage collector from
cleaning up the memory, because there will always be a valid reference
chain.  If this is the behavior you want, or if you are aware of it and
will break the chain as needed (usually best done in the __del__
method), then it is not needingly bad.

For more information on object creation and collection, see Chapter 3 of
the Python Language Reference.
(http://www.python.org/doc/ref)

-dj

Dave Trombley
<badzen at yifan.net>



More information about the Python-list mailing list