"is" and "=="

Arne Koewing ark at gmx.net
Tue Mar 11 17:24:06 CET 2003


Dagur Páll Ammendrup <dagurp at heimsnet.is> writes:

> I was wondering when == should be used and when "is". I like using
> "is" when comparing strings and stuff but maybe I shouldn't?

'is' checks object identity (that's id(a) == id(b)

for small strings may be "reused" (since they are immutable):
 
>>> s1='a'
>>> s2='b'
>>> a is b
1
>>> a == b
1

>>> s3='g'*60
>>> s4='g'*60
>>> s3 is s4
0
>>> s3 == s4
1


Arne




More information about the Python-list mailing list