Confusion about dictionaries - keys use value or identity?

Just van Rossum just at letterror.com
Sun Jul 8 15:54:53 EDT 2001


Paul Prescod wrote:
> 
> Roy Smith wrote:
> >..
> > Is there a way to force the comparison to be by identity?
> 
> Not on strings!
> 
> > I'm
> > contemplating building a cache (using a dictionary), and key comparison by
> > identity should be significantly faster than by value, because I'm going to
> > be using rather long strings as keys.
> 
> Here's one way:
> 
> >>> class a:
> ...     def __init__(self, val):
> ...         self.val = val
[ ... ]

But string comparison checks identity first, so comparing a string to
itself is already as fast as can be.

Just



More information about the Python-list mailing list