Message222607
This is a natural consequence of Python using reference semantics.
x = y
just makes x and y references to the same object. For immutable objects like int and str, you'll never notice consequences of this, since changes to the value (x += 1) replace the reference in x with a new reference. But for mutable objects like lists, you need to explicitly copy (shallow or deep) to avoid a dependency of the sort you've encountered.
For the specific case of sequences, the empty slice is the simplest, fastest way to perform a shallow copy:
x = y[:]
For other built-in types, you can often call .copy() or wrap in the constructor:
newdict = olddict.copy() # or dict(olddict)
For more complex cases, the copy module offers shallow and deep copy abilities (via the copy and deepcopy function) for arbitrary data structures. |
|
Date |
User |
Action |
Args |
2014-07-09 10:22:59 | josh.r | set | recipients:
+ josh.r, Toni Diaz |
2014-07-09 10:22:59 | josh.r | set | messageid: <1404901379.93.0.567275012433.issue21943@psf.upfronthosting.co.za> |
2014-07-09 10:22:59 | josh.r | link | issue21943 messages |
2014-07-09 10:22:59 | josh.r | create | |
|