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 eric.smith, josh.r, wyz23x2
Date 2020-12-16.06:27:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1608100043.17.0.560654515432.issue42646@roundup.psfhosted.org>
In-reply-to
Content
If you're annoyed by having to use two lines, one to copy, one to call the mutating method, you can use the walrus operator:

(y := x.copy()).some_method()

or:

(y := deepcopy(x)).some_method()

Does that cover your use case?

For the list case, you'd normally just do:

arr = lis[::-1]

but:

(arr = lis.copy()).reverse()

also works.

Granted, not super pretty. But I'm not seeing enough cases where this ugliness is truly unavoidable (the two lines don't bother me that much, and for built-ins, there is usually a one-liner that works fine, e.g. the reversing slice as shown, sorted over list.sort, etc.).

I'll note: Unconditionally calling copy.copy is fine; it knows to try the __copy__ method of the things it is called on (and most things that offer copy alias it to __copy__ or are special-cased in copy.copy as well; if they don't, they should), so you're unlikely to need to perform the "try method, fall back to copy.copy" yourself.
History
Date User Action Args
2020-12-16 06:27:23josh.rsetrecipients: + josh.r, eric.smith, wyz23x2
2020-12-16 06:27:23josh.rsetmessageid: <1608100043.17.0.560654515432.issue42646@roundup.psfhosted.org>
2020-12-16 06:27:23josh.rlinkissue42646 messages
2020-12-16 06:27:22josh.rcreate