what does is ?

Gerhard Häring gerhard.haering at opus-gmbh.net
Wed Nov 27 07:49:48 EST 2002


Padraig Brady <Padraig at Linux.ie> wrote:
> Gerhard Häring wrote:
>> Jonas Geiregat <kemu at sdf-eu.org> wrote:
>> 
>>>I saw something
>>>import string
>>>string._StringType is str
>>>what does this specially that IS
>> 
>> The "is" operator compares object *identity*, rather than equality, which
>> the "==" operator does.
> 
> Well why does the following happen on 2.2?
> Is Python being clever about merging variables?
> 
> s1="123"
> s2=s1[:]
> s1 is s2 #true?

Yes. Certain strings (not all) are being "interned". And certain ints (not all)
are interned, too:

 >>> a = 5
 >>> b = 10/2
 >>> a is b
 1
 >>> x = 1000000000000
 >>> y = 2000000000000 / 2
 >>> x is y
 0

But it seems to me that the [:] slicing has an additional optimization, apart
from interning.
-- 
Gerhard Häring
OPUS GmbH München
Tel.: +49 89 - 889 49 7 - 32
http://www.opus-gmbh.net/



More information about the Python-list mailing list