Comparing strings from the back?
Oscar Benjamin
oscar.j.benjamin at gmail.com
Mon Sep 10 10:06:16 EDT 2012
On 2012-09-10, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
> On Mon, 10 Sep 2012 08:59:37 +0000, Duncan Booth wrote:
>
>> Gelonida N <gelonida at gmail.com> wrote:
>>
>> so at the expense of a single dictionary
>> insertion when the string is created you can get guaranteed O(1) on all
>> the comparisons.
>
> 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?
Oscar
More information about the Python-list
mailing list