Comparing strings from the back?

Duncan Booth duncan.booth at invalid.invalid
Tue Sep 11 05:51:54 EDT 2012


Oscar Benjamin <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.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list