gh-108751: Add copy.replace() function#108752
Conversation
It creates a modified copy of an object by calling the object's __replace__() method. It is a generalization of dataclasses.replace(), named tuple's _replace() method and replace() methods in various classes, and supports all these stdlib classes.
e0d9d26 to
07dd17c
Compare
September 1, 2023 11:16
|
There's a new commit after the PR has been approved. @rhettinger: please review the changes made to this pull request. |
Sorry, something went wrong.
|
This change introduced a regression: see issue #109052. With this change, |
Sorry, something went wrong.
|
Would it make sense to add support for dict? >>> d={'x': 1}
>>> copy.replace(d, x=2)
TypeError: replace() does not support dict objectsIt would be an alternative to: >>> d={'x': 1}
>>> dict(d, x=2)
{'x': 2}
>>> {**d, 'x': 2}
{'x': 2} |
Sorry, something went wrong.
|
Nice feature. |
Sorry, something went wrong.
Perhaps no, because it erodes the difference between an object and a dict (as in JavaScript). And you already have other ways to do this with dicts. |
Sorry, something went wrong.
It creates a modified copy of an object by calling the object's
__replace__()method.It is a generalization of dataclasses.replace(), named tuple's _replace() method and replace() methods in various classes, and supports all these stdlib classes.
📚 Documentation preview 📚: https://cpython-previews--108752.org.readthedocs.build/