Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support of SimpleNamespace in copy.replace() #109174

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

Closed
serhiy-storchaka opened this issue Sep 9, 2023 · 1 comment
Closed

Add support of SimpleNamespace in copy.replace() #109174

serhiy-storchaka opened this issue Sep 9, 2023 · 1 comment
Labels
type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Sep 9, 2023

Feature or enhancement

SimpleNamespace is used as a simple data record type, a simpler alternative to named tuples and dataclasses. Although it is easy to create a copy of a SimpleNamespace instance with modified attributes (SimpleNamespace(**vars(ns), attr=newvalue)), it would be convenient if SimpleNamespace be supported in copy.replace() which already supports named tuples and dataclasses.

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

Linked PRs

@serhiy-storchaka
Copy link
Member Author

The implementation in #109175 is equivalent to

def __replace__(self, /, **kwargs):
    result = type(self)()
    result.__dict__.update(self.__dict__)
    result.__dict__.update(kwargs)
    return result

It does not pass arguments to the constructor, but this is consistent with pickling and copying. In any case, a SimpleNmaespace subclass with constructor is very unusual.

cc @ericsnowcurrently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant