Clarification on Immutability please

Chris Angelico rosuav at gmail.com
Mon Jan 27 10:54:22 EST 2020


On Tue, Jan 28, 2020 at 2:44 AM Souvik Dutta <souvik.viksou at gmail.com> wrote:
>
> If the two objects have the same value that means that when called all of them will point to the same object rather than creating the same value - object pairs twice. Immutability is the characteristics that does not let you change the value once set. Example - a = 5
> b = 5
> Then when a and b are called rather than saving two different objects with the same value python just creates one value and then let's two or more objects to point to the same value.
>

(I'm going to assume you meant that for the list rather than a personal email.)

If you have two independent constants that happen to have the same
value, the compiler is free to share them, but it's not required to.
Current versions of CPython do share small integers and some strings,
but not all - if you used, say, 5000000 instead of 5, they would quite
possibly be distinct objects. Other Python interpreters are free to
have different optimization rules. For instance, it's absolutely legit
to intern every string on creation, thus guaranteeing that any two
equal strings will be the same object; I don't know of any interpreter
that does this, but it's completely legal according to the
specification.

ChrisA


More information about the Python-list mailing list