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 josh.r
Recipients Toni Diaz, josh.r
Date 2014-07-09.10:22:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1404901379.93.0.567275012433.issue21943@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2014-07-09 10:22:59josh.rsetrecipients: + josh.r, Toni Diaz
2014-07-09 10:22:59josh.rsetmessageid: <1404901379.93.0.567275012433.issue21943@psf.upfronthosting.co.za>
2014-07-09 10:22:59josh.rlinkissue21943 messages
2014-07-09 10:22:59josh.rcreate