A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/python/cpython/issues/108751 below:

Add generalized replace() function · Issue #108751 · python/cpython · GitHub

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

erlend-aasland and MazyodNeilGirdhar, it-is-wednesday and Mazyod


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4