Result of ``a is b''

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Mon Mar 15 19:55:04 EST 2004


> From: Axel Boldt
> 
> is there a rationale for the following behavior:
> 
>   >>> a = (1,2)
>   >>> b = (1,2)
>   >>> a is b
>   False
>   >>> a = "12"
>   >>> b = "12"
>   >>> a is b
>   True

The implementation is free to reuse known immutable object if it desires. In CPython for example, small integers will be reused (-10..99 IIRC) and identical string constants will be reused. The empty tuple will be reused, but other tuples won't.

All this is entirely up to the implementation, which may do it for memory or performance reasons. You cannot rely on `is` for immutable objects unless you have explicitly bound the names i.e.

    a = (1, 2)
    b = a

Tim Delaney




More information about the Python-list mailing list