Referencing a container object's attributes

Dave Cole djc at object-craft.com.au
Sat Mar 24 22:50:51 EST 2001


>>>>> "Tim" == Tim Churches <tchur at optushome.com.au> writes:

>> Is so, there there is no language feature that would allow you to
>> do this without passing an instance of 'b' to 'a'. Standard
>> practice would be to do something like this:
>> 
>> class a:
>>     def __init__(self, parent):
>>         self.parent = parent
>> 
>> def doSomething(self):
>>     self.parent.blah( )
>> 
>> class b:
>>     __init__(self):
>>         self.a = a( self )
>> 

Tim> OK, many thanks.

Tim> One question: does the circularity inherent in the above (in
Tim> which a reference to the container object is placed in an
Tim> attribute of each containee) have any potential for causing
Tim> problems - particularly since I want to pickle the container
Tim> objects?

You should be able to pickle the container, but you will have to
manually remove all of the circular references yourself when you want
to free the object(s).

In Python 2.x I understand that the garbage collector can detect and
handle circular references.

- Dave

-- 
http://www.object-craft.com.au



More information about the Python-list mailing list