Iterating over objects of a class

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Dec 24 19:48:06 CET 2008


En Wed, 24 Dec 2008 16:18:55 -0200, Kottiyath <n.kottiyath at gmail.com>  
escribió:

>> The other thing to remember is that because the 'registry' contains
>> references to the instances, they won't be garbage collected.
>
> Is there any other way out in this case?
> I have factory methods - and I have to loop over them - sort of Chain
> of Responsibility pattern.
> Having a registry inside the class instance and looping through them
> was the only clean thing I could think of.
> I understand that garbage collection would be an issue - but is there
> any way out?

You may keep all that structures - just use weak references (see the  
weakref module).
There isn't a WeakList nor WeakSet out-of-the-box but you may use a  
WeakKeyDictionary (set the value to anything, None by example).

-- 
Gabriel Genellina




More information about the Python-list mailing list