Comparing strings from the back?
Terry Reedy
tjreedy at udel.edu
Tue Sep 11 11:55:55 EDT 2012
On 9/11/2012 6:40 AM, Oscar Benjamin wrote:
> On 11 September 2012 10:51, Duncan Booth <duncan.booth at invalid.invalid
> <mailto:duncan.booth at invalid.invalid>> wrote:
>
> Oscar Benjamin <oscar.j.benjamin at gmail.com
> <mailto:oscar.j.benjamin at gmail.com>> wrote:
>
> >> What interning buys you is that "s == t" is an O(1) pointer compare
> >> if they are equal. But if s and t differ in the last character,
> >> __eq__ will still inspect every character. There is no way to tell
> >> Python "all strings are interned, if s is not t then s != t as
> well".
> >>
> >
> > I thought that if *both* strings were interned then a pointer
> > comparison could decide if they were unequal without needing to check
> > the characters.
> >
> > Have I misunderstood how intern() works?
> >
>
> I don't think you've misunderstood how it work, but so far as I can
> see the
> code doesn't attempt to short circuit the "not equal but interned" case.
> The comparison code doesn't look at interning at all, it only looks for
> identity as a shortcut.
>
>
> It also doesn't seem to check if the hash values have been set. I guess
> the cached hash value is only used in contexts where the hash is
> explicitly desired.-
I believe the internal use of interning and hash comparison has varied
from release to release. However, the main use of string comparison is
for dict keys, especially the internal dicts for namespaces and
attributes. Since the dict lookup code needs hash values anyway, to find
slots with possible conflicts, I am sure it does not use the generic
comparison operators but starts with hash comparisons.
Terry Jan Reedy
More information about the Python-list
mailing list