[Tutor] Identity operator (basic types)

John Fouhy john at fouhy.net
Sat Feb 10 06:46:48 CET 2007

On 10/02/07, Cecilia Alm <ebbaalm at uiuc.edu> wrote:
> Why does the identity operator return "True" in the below cases, that is
> when assigning  the same value to basic variable types (float, integer,
> string, bool..)? Are these rcopied by reference (shallow)? If so why?
>  >>> i = 10
>  >>> j = 10
>  >>> i is j
>  True

Effectively, yes.

Integers in python are immutable.  I'm not sure exactly what happens
under the hood, but you can think of it like there is only one 10 in
the interpreter's memory, and every name you give it is just a pointer
to that one 10.  Remember, python is fully OO -- _everything_ is a
reference :-)

(this is also why there is no ++ operator)


More information about the Tutor mailing list