Immutable and Mutable Types

Diez B. Roggisch deets at
Mon Mar 17 11:09:27 CET 2008

cokofreedom at wrote:

>> >>> a = 1
>> >>> b = 1
>> >>> a is b
>> True
>> >>> id(a)
>> 10901000
>> >>> id(b)
>> 10901000
> Isn't this because integers up to a certain range are held in a single
> memory location, thus why they are the same?

As the OP said:

Depending on implementation, for immutable types, operations that compute 
new values may or may not actually return a reference to any existing object 
with the same type and value, while for mutable objects this is (strictly)? 
not allowed.

Which is exactly what happens - the actual implementation chose to cache
some values based on heuristics or common sense - but no guarantees are
made in either way.


More information about the Python-list mailing list