python string comparison oddity

Duncan Booth duncan.booth at invalid.invalid
Thu Jun 19 06:49:17 EDT 2008


Faheem Mitha <faheem at email.unc.edu> wrote:

>>> In [1]: a = '--'
>>>
>>> In [2]: a is '--'
>>> Out[2]: False
>>>
>>> In [4]: a = '-'
>>>
>>> In [5]: a is '-'
>>> Out[5]: True
>>>
>>> In [6]: a = 'foo'
>>>
>>> In [7]: a is 'foo'
>>> Out[7]: True
>>
>> Yes, this happens because of small objects caching. When small
>> integers or short strings are created, there are possibility that they
>> might refer to the same objects behind-the-scene. Don't rely on this
>> behavior.
> 
> Yes, but why is '-' and 'foo' cached, and not '--'? Do you know what
> the basis of the choice is?

Also note that the behaviour you saw above changes if you put code into a 
script rather than running it interactively (the string '--' will be re-
used within a single compilation unit). So even if you understand all of 
the choices made in your particular release of Python (and they do vary 
between releases) it would be very unwise to rely on this behaviour.

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



More information about the Python-list mailing list