instance references?
Alex Martelli
aleax at mail.comcast.net
Sun Jan 29 23:44:56 EST 2006
bytecolor <bytecolor at yahoo.com> wrote:
> Thanks Alex, the weak references *seem* to be doing what I want for
> now.
>
> In each __init__() I use:
> aptbase.drawables.append(weakref.ref(self))
>
> Then in show():
> for d in aptbase.drawables:
> o = d()
> if o is not None:
> # render it
Looks good, so apparently you didn't particularly care about assignment
to variables vs assignment to other "labels" -- great!
My favourite way to use weakref is slightly different: I would have
aptbase.drawables = weakref.WeakValueDictionary
then in each __init__
aptbase.drawables[len(aptbase.drawables)] = self
then in show:
for o in aptbase.drawables.values():
# render it
Not a vastly different idiom from yours, mind you, but I like not having
to check whether an object has gone away -- a WeakValueDictionary's
entries just disappear when the value object goes away, so that at any
time looping on its .values() is safe as brick houses!-)
Alex
More information about the Python-list
mailing list