instance references?

Alex Martelli aleax at mail.comcast.net
Mon Jan 30 21:58:11 EST 2006


Scott David Daniels <scott.daniels at acm.org> wrote:

> Alex Martelli wrote:
> > My favourite way to use weakref is slightly different: I would have
> >    aptbase.drawables = weakref.WeakValueDictionary
> typo here:
>       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
> The keys you are choosing are probably not a good idea.
> Problem demo:
> 
>     a,b,c = Something(), Something(), Something()
>     b = None
>     d = Something()  # overwrites the d entry.
> 
> I'd use:
>      aptbase.drawables[id(self)] = self

Good point -- the id isn't going to be reused until self is gone, of
course.  I normally use a monotonically increasing counter (global or in
self's class), but there's no real reason for that -- id is simpler (and
using len as I had is definitely bugprone, as you mention).


Alex



More information about the Python-list mailing list