Immutable and Mutable Types

Dan Bishop danb_83 at yahoo.com
Mon Mar 17 02:51:14 EDT 2008


Bernard Lim wrote:
> Hi,
>
> I'm reading the Python Reference Manual in order to gain a better understanding
> of Python under the hood.
>
> On the last paragraph of 3.1, there is a statement on immutable and mutable
> types as such:
>
> <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>
>
> Using the example given in 3.1, how do I verify that this is true pertaining
> to immutable types? Below is my understanding on verifying the above statement:
>
> >>> a = 1
> >>> b = 1
> >>> a is b
> True
> >>> id(a)
> 10901000
> >>> id(b)
> 10901000
>
> Is this correct?

Yes, that's correct.  However,

>>> a = 100
>>> b = 100
>>> a is b
False
>>> id(a)
135644988
>>> id(b)
135645000



More information about the Python-list mailing list