Message383117
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. |
|
Date |
User |
Action |
Args |
2020-12-16 06:27:23 | josh.r | set | recipients:
+ josh.r, eric.smith, wyz23x2 |
2020-12-16 06:27:23 | josh.r | set | messageid: <1608100043.17.0.560654515432.issue42646@roundup.psfhosted.org> |
2020-12-16 06:27:23 | josh.r | link | issue42646 messages |
2020-12-16 06:27:22 | josh.r | create | |
|