Tuple assignment and generators?

Duncan Booth duncan.booth at invalid.invalid
Fri May 5 12:44:24 CEST 2006

Daniel Nogradi wrote:

>>>> a = 10
>>>> b = 10
>>>> id(a)
> 134536516
>>>> id(b)
> 134536516
> So the two memory addesses are the same, but
>>>> a = 10000
>>>> b = 10000
>>>> id(a)
> 134604216
>>>> id(b)
> 134604252
> and they are not the same (I restarted the interpreter between the two
> cases). So how is this now? Sorry if it's too trivial, but I simply
> don't get it.
If two immutable values are the same, then the interpreter has the right to 
simply reuse the same value. Apart from the guarantee that it will do this 
with None everything else is left open.

The current C-Python implementation will reuse small integers but not large 
integers, it also reuses some strings. It reuses the empty tuple but not 
(so far as I know) any other tuples. This could change at any time and 
other Python implementations may do totally different things here.

Just because you saw it reusing a small value such as 10 doesn't mean that 
there cannot be other small integers with the value 10 which aren't the 
same as that one. Back before Python had a separate bool type, it used to 
use two different integers for 0 (and two for 1), so you could (by an 
accident of implementation) tell whether a value had been created by a 
comparison operator. So far as I know, there is nothing stopping the author 
of an extension written in C continuing to create their own versions of 
small numbers today.

More information about the Python-list mailing list