Skip to content
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

Add generalized replace() function #108751

Closed
serhiy-storchaka opened this issue Sep 1, 2023 · 0 comments
Closed

Add generalized replace() function #108751

serhiy-storchaka opened this issue Sep 1, 2023 · 0 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Sep 1, 2023

Feature or enhancement

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/generalize-replace-function/28511

Proposal:

Some classes have the replace() method, which creates a modified copy of the object (modified values are provided as keyword arguments). Named tuples have the _replace() for this (to avoid conflict with attribute replace). Dataclasses provide a global function for this.

I proposed to generalize dataclasses.replace() to support all classes which need this feature. By the result of the discussion on discuss.python.org, the new function will be added in the copy module as copy.replace(). dataclasses.replace() will continue to support only dataclasses. Dataclasses, named tuples and all classes which currently have the replace() method with suitable semantic will get also the __replace__() method. Now you can add such feature in new classes without conflicting with the replace attribute, and use this feature in general code without conflicting with str.replace() and like.

For now, copy.replace() is more limited than copy.copy() and does not fall back to use the powerful pickle protocol.

Linked PRs

@serhiy-storchaka serhiy-storchaka added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Sep 1, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Sep 1, 2023
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.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Sep 1, 2023
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.
serhiy-storchaka added a commit that referenced this issue Sep 6, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

1 participant