Copying versus aliasing
Two names for one object, a shallow copy, a deep copy, or a NumPy view. Whether a change in one place shows up in another depends on which you have.
Every Python name points at an object. The question is always is this a second name for the same object, or a different object?
b = anever copies. Both names point at one object, so if it's mutable (a list, dict or set), a change throughbshows up ina.- Passing a list to a function is the same: the function gets another name for your list, not a copy.
- You want an independent outer list → a shallow copy:
a.copy(),list(a)ora[:]. Nested lists inside are still shared. - You want nothing shared at any depth →
copy.deepcopy(a). - You're checking "same object?" rather than "same value?" → use
is, not==. - NumPy slices are views of the same memory; call
.copy()when you need a separate array.
Immutable values (numbers, strings, tuples of immutables) can be shared safely, because nothing can change them in place.
Names and objects
A name is a label stuck on an object, not a box holding a value.
is asks "same object?"; == asks "same value?".
When a change shows up elsewhere
Section
Changing lists
Methods like append and sort change the list in place and return None.
Section
Nesting, mutability and copying
Shallow versus deep copies, and the [[0] * 3] * 3 trap.
Section
Parameters and arguments
An argument is passed as a reference to the caller's object, and a mutable default is shared between calls.
Arrays
A slice of an array is a view; fancy indexing makes a copy.