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

Peter J. Holzer hjp-python at hjp.at
Wed Jun 16 17:11:07 EDT 2021


On 2021-06-16 18:32:46 +0200, Dieter Maurer wrote:
> Chris Angelico wrote at 2021-6-16 02:20 +1000:
> >On Wed, Jun 16, 2021 at 2:18 AM Dieter Maurer <dieter at handshake.de> wrote:
> >> As far as I know, there are no guarantees are the language level.
> >> There are some (partially documented) implementation details
> >> for CPython (which is just one possible implementation).
> >
> >Yes there are - plenty of them :) The example I gave is a language
> >guarantee.
> 
> Would you point to the documentation location where this is guaranteed?

I think it's in https://docs.python.org/3/reference/expressions.html#atom-identifiers

| When the name is bound to an object, evaluation of the atom yields that object.

So in

    x = whatever
    a_list = [x for _ in range(3)]

you get a list of 3 references to the same object, not 3 references to 3
different copies of that object.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20210616/88bbd06c/attachment.sig>


More information about the Python-list mailing list