Surprise using the 'is' operator

Diez B. Roggisch deets at nospam.web.de
Tue Sep 26 05:37:23 EDT 2006


codefire wrote:

> I thought the 'is' operator was used to identify identical objects,
> whereas the '==' operator checked equality. Well, I got a surprise
> here:
> 
> IDLE 1.1.3
>>>> a = 10
>>>> b = a
>>>> a is b
> True
>>>> a == b
> True
>>>> c = 10
>>>> a == c
> True
>>>> a is c
> True
>>>>
> 
> I was NOT expecting the last statement to return True!
> 
> What am I missing?

One of the most severely beaten dead horses of python - small integers are
cached by the interpreter. Maybe. Thus the above results.

Google for a bazillion discussions on this.

Diez



More information about the Python-list mailing list