Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add generalized replace() function #108751

Closed
Labels
stdlibPython modules in the Lib dirtype-featureA feature request or enhancement
@serhiy-storchaka

Description

@serhiy-storchaka

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 thereplace() 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 attributereplace). Dataclasses provide a global function for this.

I proposed to generalizedataclasses.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 thecopy module ascopy.replace().dataclasses.replace() will continue to support only dataclasses. Dataclasses, named tuples and all classes which currently have thereplace() method with suitable semantic will get also the__replace__() method. Now you can add such feature in new classes without conflicting with thereplace attribute, and use this feature in general code without conflicting withstr.replace() and like.

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

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp