index all instances by id - a memory leak?

Richard Levasseur richardlev at gmail.com
Thu Oct 2 18:34:45 CEST 2008


On Oct 1, 10:35 pm, Jason Scheirer <jason.schei... at gmail.com> wrote:
> On Oct 1, 10:01 pm, Dan Barbus <dan.bar... at gmail.com> wrote:
>
> > On Oct 2, 7:54 am, Dan Barbus <dan.bar... at gmail.com> wrote:
>
> > >     def getItemById(id):
> > >         return _itemsById[id]
>
> > I just saw that this won't compile. Still, ignoring this, I thing the
> > purpose of the code is pretty clear.
>
> > Thanks for any feedback.
>
> Check the weakref module, you can create a WeakValueDictionary which
> will not increment the refcount of the value, and will return None
> after it's been garbage collected.

Another idea: have an AbstractFactory that keeps track of instances
created.  When you done, delete the abstract factory, which will
delete the internal index.  You'll have to figure out how to pass the
factory around, and when to delete it.  Weakref sounds better, overall.



More information about the Python-list mailing list