aliasing
Having two or more names that refer to the same object, so a change made through one name shows up through the other.
In more detail
Assignment such as b = a never copies; it makes b another name for the same object. With immutable values that is harmless, but with a list or a graphics shape, changing b changes a too. Make a separate copy with clone(), list(a), a[:] or copy.copy when you need one.
Example
a = [1, 2]
b = a
b.append(3)
print(a) # [1, 2, 3]Related terms
- mutable — Able to be changed in place after it is created, like most lists, arrays and objects in languages such as Python and JavaScript.
- clone() — Making a separate, independent copy of an object, such as shape.clone() in graphics.py or a list copy with list(a).
- variable — A name that refers to a value, created by assignment, such as width = 10.
Taught in
- Installing Python On Windows — Hands-on Python: Complete Video Course & Book
- First Run, And Fixing A Broken Install — Hands-on Python: Complete Video Course & Book
- Mutable Objects And Aliases — Hands-on Python: Complete Video Course & Book
- Thirty Questions, One Sneaky Print — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Control Flow Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Lists: Slices, Methods And Aliases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Twelve Traps, One Shape Each — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Import Variants, dir() And sys.path — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Modules Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
References
- Python FAQ: why did changing list y also change list x?
- Python tutorial: a word about names and objects