[issue45169] Shallow copy occurs when list multiplication is used to create nested lists; can confuse users

2021-09-10 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

Oh, I forgot: this exact issue is also in the FAQs.

https://docs.python.org/3/faq/programming.html#id46

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45169] Shallow copy occurs when list multiplication is used to create nested lists; can confuse users

2021-09-10 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

This is not a bug. No copy is made at all, neither shallow nor deep.

This is described in the documentation for built-in types:

https://docs.python.org/3/library/stdtypes.html#common-sequence-operations

and is similar to the issue in the FAQs:

https://docs.python.org/3/faq/programming.html#id16


I acknowledge that this is sometimes confusing for beginners, but it is just 
one of those things that programmers have to learn. Sequence multiplication 
does not copy the items, it replicates references to the same item.

--
nosy: +steven.daprano
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45169] Shallow copy occurs when list multiplication is used to create nested lists; can confuse users

2021-09-10 Thread 2106 Hyunwoo Oh


Change by 2106 Hyunwoo Oh :


--
components: +Interpreter Core -Demos and Tools

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45169] Shallow copy occurs when list multiplication is used to create nested lists; can confuse users

2021-09-10 Thread 2106 Hyunwoo Oh


New submission from 2106 Hyunwoo Oh :

If you do the following:

lists=[[]]*100
lists[1].append('text')
print(lists[2])

you can see lists[2] contains 'text' even though it was appended to lists[1] in 
the text. A little more investigation with the id() function can show that the 
lists are shallowly copied when list multiplication occurs. I think this can 
confuse users when they try to use list multiplication to create nested lists, 
as they expected a deep copy to occur.

--
components: Demos and Tools
messages: 401625
nosy: ohwphil
priority: normal
severity: normal
status: open
title: Shallow copy occurs when list multiplication is used to create nested 
lists; can confuse users
type: behavior
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com