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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchakaserhiy-storchaka commentedSep 1, 2023
edited by github-actionsbot
Loading

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/

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 thesestdlib classes.
single: __replace__() (replace protocol)

Function :func:`replace` is more limited than :func:`copy` and :func:`deepcopy`,
and only supports named tuples, dataclasses, and other classes which
Copy link
Member

@merwokmerwokSep 1, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
and only supports named tuples,dataclasses, and other classes which
and only supports:term:`named tuples <named tuple>`,:mod:`dataclasses`, and other classes which

(I haven’t double checked ifnamed tuple ornamedtuple is the right key)

Copy link
MemberAuthor

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 bycollections.namedtuple().

func = getattr(cls, '__replace__', None)
if func is None:
raise TypeError(f"replace() does not support {cls.__name__} objects")
return func(obj, **changes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is a cool feature for very little code!

erlend-aasland, heckad, AmauryCarrade, mattyclarkson, it-is-wednesday, KubaO, and pelson reacted with thumbs up emoji
@bedevere-bot
Copy link

There's a new commit after the PR has been approved.

@rhettinger: please review the changes made to this pull request.

@vstinner
Copy link
Member

This change introduced a regression: see issue#109052.

With this change,test_sys_settrace started to crash. I don't know if the bug already existed before and was hidden, or if theObjects/codeobject.c change really introduced a regression.

----

* Add :func:`copy.replace` function which allows to create a modified copy of
an object, which is especially usefule for immutable objects.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

usefule -> useful

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks. Do you mind to create a PR to fix this typo?

@vstinner
Copy link
Member

Would it make sense to add support for dict?

>>>d={'x':1}>>>copy.replace(d,x=2)TypeError:replace()doesnotsupportdictobjects

It would be an alternative to:

>>>d={'x':1}>>>dict(d,x=2){'x':2}>>> {**d,'x':2}{'x':2}

@vstinner
Copy link
Member

Nice feature.

@serhiy-storchaka
Copy link
MemberAuthor

Would it make sense to add support for dict?

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.

merwok, vstinner, AlexWaygood, and erlend-aasland reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@merwokmerwokmerwok left review comments

@csm10495csm10495csm10495 left review comments

@ericvsmithericvsmithAwaiting requested review from ericvsmithericvsmith is a code owner

@pgansslepganssleAwaiting requested review from pgansslepganssle is a code owner

@abalkinabalkinAwaiting requested review from abalkinabalkin is a code owner

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

@rhettingerrhettingerAwaiting requested review from rhettingerrhettinger is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@serhiy-storchaka@bedevere-bot@vstinner@merwok@rhettinger@csm10495

[8]ページ先頭

©2009-2025 Movatter.jp