gc questions

Terry Reedy tjreedy at udel.edu
Thu Jun 19 22:43:20 CEST 2003


"Jeremy Hylton" <jeremy at zope.com> wrote in message
news:mailman.1056041615.9951.python-list at python.org...
> On Wed, 2003-06-18 at 20:23, Edward K. Ream wrote:
> > Many thanks, Jeremy, for these hints.  Yes, a dictionary speeds
things up a
> > lot.  dict[o] = o won't work for unhashable objects o, so here is
the code I
> > use.  It seems reliable and fast:
> >
> >     global lastObjectsDict
> >
> >     objects = gc.get_objects()
> >     newObjects = [o for o in objects if not
lastObjectsDict.has_key(id(o))]
> >
> >     lastObjectsDict = {}
> >     for o in objects:
> >         lastObjectsDict[id(o)]=o
> >
> >     print "%d new, %d total objects" %
(len(newObjects),len(objects))
>
> The dict using id() trick is convenient in practice, but it has some
> problems.  It's possible for an address to get re-used, so that two
> different objects get the same id() on two different invocations of
this
> code.  I expect this is pretty likely for objects that are
implemented
> with their own free lists.  Whether this is a problem or not depends
on
> what you are trying to do.

I once thought of the same trick, before I realized that uniquness is
only guaranteed for any partiuclar moment in time.  'Possible' is
quite easy to bring about when objects are allow to be deleted

>>> id(()), id(())
(7669904, 7669904)
>>> id([]), id([])
(7947216, 7947216)
>>> id({}),id({})
(7946592, 7946592)
>>> id(1001), id(1001)
(7691524, 7691524)

but will not happen when the object is strored as part of the value
corresponding to its id key, as above.

Terry J. Reedy






More information about the Python-list mailing list