Immutable and Mutable Types

Diez B. Roggisch deets at nospam.web.de
Mon Mar 17 06:09:27 EDT 2008


cokofreedom at gmail.com 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:

<paraphrase>
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.
</paraphrase>

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.

Diez



More information about the Python-list mailing list