On 9/11/2012 6:40 AM, Oscar Benjamin wrote:
On 11 September 2012 10:51, Duncan Booth <duncan.booth@invalid.invalid
<mailto:duncan.booth@invalid.invalid>> wrote:

    Oscar Benjamin <oscar.j.benja...@gmail.com
    <mailto:oscar.j.benja...@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

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to