storing meta data on dictionary keys

Andreas Kraemer akraemer at sbcglobal.net
Thu Oct 11 12:27:37 EDT 2007


> > [...]                        In fact, now that I think of it, get_key
> > is probably a bad name for it, get_other_object_with_this_same_key is
> > probably more apt :)
>
> Or more precise:
> get_key_that_was_used_when_value_was_inserted_into_dictionary :-)

Or even more precisely:

get_key_object_that_was_used_when_the_initial_value_belonging_to_that_key_was_inserted_into_dictionary

since rebinding other values to the stored key won't change it:

class Str(str):
  def __repr__(self):
    return str(self)+': '+self.tag

>>> a = Str('foo')
>>> a.tag = "I'm the first one!"
>>> b = Str('foo')
>>> b.tag = "I'm not."
>>> d = {}
>>> d[a] = 'bar'
>>> d[b] = 'spam'
>>> d.items()[0]
(foo: I'm the first one!, 'spam')





More information about the Python-list mailing list