-
- Notifications
You must be signed in to change notification settings - Fork 33.5k
gh-108751: Add copy.replace() function #108752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -115,6 +115,18 @@ array | |
| It can be used instead of ``'u'`` type code, which is deprecated. | ||
| (Contributed by Inada Naoki in :gh:`80480`.) | ||
| | ||
| copy | ||
| ---- | ||
| | ||
| * Add :func:`copy.replace` function which allows to create a modified copy of | ||
| an object, which is especially usefule for immutable objects. | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usefule -> useful Member Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Do you mind to create a PR to fix this typo? | ||
| It supports named tuples created with the factory function | ||
| :func:`collections.namedtuple`, :class:`~dataclasses.dataclass` instances, | ||
| various :mod:`datetime` objects, :class:`~inspect.Signature` objects, | ||
| :class:`~inspect.Parameter` objects, :ref:`code object <code-objects>`, and | ||
| any user classes which define the :meth:`!__replace__` method. | ||
| (Contributed by Serhiy Storchaka in :gh:`108751`.) | ||
| | ||
| dbm | ||
| --- | ||
| | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -290,3 +290,16 @@ def _reconstruct(x, memo, func, args, | |
| return y | ||
| | ||
| del types, weakref | ||
| | ||
| | ||
| def replace(obj, /, **changes): | ||
| """Return a new object replacing specified fields with new values. | ||
| | ||
| This is especially useful for immutable objects, like named tuples or | ||
| frozen dataclasses. | ||
| """ | ||
| cls = obj.__class__ | ||
| func = getattr(cls, '__replace__', None) | ||
| if func is None: | ||
| raise TypeError(f"replace() does not support {cls.__name__} objects") | ||
| return func(obj, **changes) | ||
| Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a cool feature for very little code! | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I haven’t double checked if
named tupleornamedtupleis the right key)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it does not support named tuples in general. It currently only supports named tuples created by
collections.namedtuple().