instance creatiion as an "event"

Alex Martelli aleax at aleax.it
Tue Apr 15 08:40:15 EDT 2003


Aahz wrote:

> In article <mailman.1050117319.5102.python-list at python.org>,
> Steven Taschuk  <staschuk at telusplanet.net> wrote:
>>Quoth Simon Burton:
>>> On Fri, 11 Apr 2003 17:15:32 -0400, Arthur wrote:
>>>>
>>>> The only thing I seem to be missing is some kind of "hook" to trigger a
>>>> general update cycle of all existing class instances upon the creation
>>>> of a new class instance at the interactive prompt
>>> 
>>> class Member:
>>> members = []
>>> def __init__(self):
>>> for m in Member.members:
>>> m.notify(self)
>>> Member.members.append(self)
>>> def notify(self,other):
>>> print self,"a new member:",other
>>
>>Though you will probably want to use weak references in
>>Member.members, unless you intend that all instances of the class
>>live as long as the class does.
> 
> Blech.  It's essentially impossible to delete a class as long as
> instances of it exist.

Of course (the instances are keeping the class alive through their
__class__ attributes), but what does that have to do with Steven's
suggestion, making it worth a "blech"?  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...?).


Alex





More information about the Python-list mailing list