This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients darmentr, paul.moore, steve.dower, steven.daprano, tim.golden, zach.ware
Date 2018-11-06.14:04:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1541513043.67.0.788709270274.issue35176@psf.upfronthosting.co.za>
In-reply-to
Content
This is not a bug, this is standard behaviour, working as designed.

'a' is not a copy of the list 'x', 'a' is another name for the same list as 'x'. Any in-place modifications you make to 'a' happens to the object itself, the list, which is visible regardless of which name you refer to it by.

If you are a C programmer, you can think of this as being similar to pointers: think of 'x' as a pointer to the list, and 'a' as a pointer to the same list. (That's more or less what happens under the hood.) From the Python level, we say that both names 'a' and 'x' refer to the same object.

If you want a copy, you can use the copy module, or for lists, you can take a slice: a = x[:] makes a copy of the list.

For immutable objects like strings, you don't need a copy, because you cannot modify them in place: any operation on a string always creates a new string, leaving the old one untouched.
History
Date User Action Args
2018-11-06 14:04:03steven.dapranosetrecipients: + steven.daprano, paul.moore, tim.golden, zach.ware, steve.dower, darmentr
2018-11-06 14:04:03steven.dapranosetmessageid: <1541513043.67.0.788709270274.issue35176@psf.upfronthosting.co.za>
2018-11-06 14:04:03steven.dapranolinkissue35176 messages
2018-11-06 14:04:03steven.dapranocreate