"is" and "=="

Arne Koewing ark at gmx.net
Tue Mar 11 11:24:06 EST 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