Why the list creates in two different ways? Does it cause by the mutability of its elements? Where the Python document explains it?

Chris Angelico rosuav at gmail.com
Tue Jun 15 20:21:23 EDT 2021


On Wed, Jun 16, 2021 at 10:17 AM Avi Gross via Python-list
<python-list at python.org> wrote:
>
> May I ask if there are any PRACTICAL differences if multiple immutable
> tuples share the same address or not?

No, there aren't. It's nothing more than an (optional) optimization.
This is true of every immutable type, including strings (both text and
byte), numbers (int, float, etc), and less obvious ones like code
objects. (Though not function objects. They're mutable.)

> I mean if I use a tuple in a set or as the key in a dictionary and a second
> one comes along, will it result in two entries one time and only one the
> other time?

Nope; dictionaries use the *value*, so even if you have two distinct
tuples with the same value, they will be considered equal.

> Some languages I use often have a lazy evaluation where things are not even
> copied when doing some things and only copied if absolutely needed as in
> when you make a change.  So you can create say a data.frame then make
> another from it with some columns added and removed and the storage used
> does not change much, but add or remove one or two, and suddenly entire
> large amounts get copied.
>
> So by that argument, you could have the copies of the list also be the same
> at first and since they are mutable, they might diverge later or not but
> tuples would never change so why not share if the compiler or interpreter
> was set to use less space when possible.

Hmm. Copy-on-write is permissible but wouldn't be worthwhile in most
cases. But I'm sure there are situations where it would be worth it.

> Now if you really still want true copies, what ways might fool a compiler?
>
> NoDup = [(5, 2), (6-1, 6/3), (12%7, 1/1 + 1/1)]
>

Note that these would all compare equal, but they are NOT
indistinguishable. The first one has a pair of integers, the other two
have an int and a float. But with the two that *are*
indistinguishable, CPython 3.10 folds them together and has two
references to the same tuple.

ChrisA


More information about the Python-list mailing list