Storing 'unhashable' types in dictionaries by address
Ed Avis
ed at membled.com
Tue May 27 16:07:48 EDT 2003
Chad Netzer <cnetzer at mail.arc.nasa.gov> writes:
>>>The builtin function id(x)
>>So now I can happily associate extra information with arbitrary
>>objects by using their address.
>
>One thing to remember is that Python is free to reuse the object id (ie.
>address), after that object is deleted.
>Of course, it is likely the case when keeping track of ids, that you
>might also have a list of the objects, or some other way to keep
>track of them all (otherwise, what value is the id()?).
Indeed. I wouldn't normally let go of an object but keep its entry in
a dictionary. Still it's possible this gotcha could be a source of
subtle bugs.
The dictionary-wrapping class I suggested, which keeps track of the
object corresponding to each id and so allows objects to be used as
dictionary keys directly, would of course take care of this.
I didn't mention it at the time but I am thinking of something very
similar to Perl's Tie::RefHash module.
--
Ed Avis <ed at membled.com>
More information about the Python-list
mailing list