instance creatiion as an "event"
aleax at aleax.it
Wed Apr 16 12:30:02 CEST 2003
Greg Ewing (using news.cis.dfn.de) wrote:
> Alex Martelli wrote:
>> If Member.members is coded
>> as above, then you have circular reference loops involving the
>> class and each instance so just about everything becomes sort of
>> "immortal" -- with weak references in Member.members, no circular
>> reference loops, no problems (or, am I missing something...?).
> Circular references aren't a disaster these days, with
> the cyclic garbage collector.
...unless something in the loop has a __del__, in which case
you're skirting disaster-area again.
But that's not really what we were talking about. It's unlikely
the class object will go away anytime soon -- most of the time
it's a top-level name in a module, or referenced by one, and you
know how persistent those are. Now, if the class object has
NON-weak references to all of the instances, those instances
become deucedly persistent too. So why should the references
from class to instances be weak, since they're NOT intended
to keep the instances alive, merely to be able to reach those
that are STILL alive for other reasons when something happens?
> But if you want to be sure things are reclaimed as
> soon as possible, weak refs are still a good idea.
More generally, weak references are useful when you want to
be able to refer to something if that something still exists,
but NOT keep that something in existence otherwise -- that's
their design intent, after all.
More information about the Python-list