[Tutor] Identity operator (basic types)

Kent Johnson kent37 at tds.net
Sat Feb 10 14:00:35 CET 2007


Cecilia Alm 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?

Assignment in Python is always by reference. Variables in Python are not 
containers for values, they are names for values. See
http://effbot.org/zone/python-objects.htm

In general it is a bad idea to use 'is'; for several reasons it can 
yield surprising results. == is usually a better choice. One exception 
is when comparing to a known singleton object, for example
   if a is None:
is a good way to test for None.

Kent

> 
>  >>> i = 10
>  >>> j = 10
>  >>> i is j
> True
> 
> 
>  >>> a = 10
>  >>> b = a
>  >>> a is b
> True
> 
> Thanks!
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list