# Generalize replace() function

**URL:** <https://discuss.python.org/t/generalize-replace-function/28511>\
**Category:** Ideas\
**Created:** [June 26, 2023, 9:42am UTC](https://discuss.python.org/t/generalize-replace-function/28511 "2023-06-26T09:42:34Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![AlexWaygood](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexwaygood/32/4722_2.png) [@AlexWaygood](https://discuss.python.org/u/AlexWaygood)\
**Post date:** [September 1, 2023, 6:26pm UTC](https://discuss.python.org/t/generalize-replace-function/28511/21 "2023-09-01T18:26:16Z")

</div>

> [@NeilGirdhar](#):
>
> Will the typeshed have an appropriate base class?

You’re not allowed to use `P.kwargs` in an annotation without `P.args`, so it will be hard for us to provide a precise signature for this function at typeshed in the way you suggest above. (FWIW, I agree that this is an awesome new feature, and don’t think that the fact that it’s difficult to have accurate type annotations for it is in any way an argument against adding the feature.)

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [September 1, 2023, 8:04pm UTC](https://discuss.python.org/t/generalize-replace-function/28511/22 "2023-09-01T20:04:09Z")

</div>

> [@AlexWaygood](#):
>
> don’t think that the fact that it’s difficult to have accurate type annotations for it is in any way an argument against adding the feature.)

I hope it didn’t sound like I was suggesting that!

Just curious, but the dependency of one type variable on another is okay?

```python
def replace[T: SupportsReplace[P], **P]

```

I wasn’t sure if that would work.

---

<div class="post-metadata">

**Author:** ![AlexWaygood](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alexwaygood/32/4722_2.png) [@AlexWaygood](https://discuss.python.org/u/AlexWaygood)\
**Post date:** [September 1, 2023, 8:15pm UTC](https://discuss.python.org/t/generalize-replace-function/28511/23 "2023-09-01T20:15:36Z")

</div>

> [@NeilGirdhar](#):
>
> I hope it didn’t sound like I was suggesting that!

It didn’t – I was just clarifying that it also was not my opinion either 😉

> [@NeilGirdhar](#):
>
> Just curious, but the dependency of one type variable on another is okay?
> 
> ```auto
> def replace[T: SupportsReplace[P], **P]
> 
> ```

Er, yeah, that’s illegal too, good point 😛

---

<div class="post-metadata">

**Author:** ![sobolevn](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sobolevn/32/9274_2.png) [@sobolevn](https://discuss.python.org/u/sobolevn)\
**Post date:** [September 27, 2023, 10:18am UTC](https://discuss.python.org/t/generalize-replace-function/28511/24 "2023-09-27T10:18:52Z")

</div>

I proposed adding `SupportsReplace` protocol to `typing` here: [Incorrect rendering of ` __replace__ ` method in `copy` docs: consider adding `typing.SupportsReplace`? · Issue #109961 · python/cpython · GitHub](https://github.com/python/cpython/issues/109961) 🙂

`**kwargs` would have to be `Any` (for now at least).

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hlovatt/32/36838_2.png) [@hlovatt](https://discuss.python.org/u/hlovatt)\
**Post date:** [August 25, 2025, 2:17am UTC](https://discuss.python.org/t/generalize-replace-function/28511/25 "2025-08-25T02:17:22Z")

</div>

`copy.replace` is great, however I keep writing:

```python
    def __replace__ (self, **kwargs: Any) -> Self: # Allow replacements.
        new = copy.deepcopy(self)
        for key, value in kwargs.items():
            setattr(new, key, value)
        return new

```

Why not add something like this to `object`?

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [August 25, 2025, 7:09am UTC](https://discuss.python.org/t/generalize-replace-function/28511/26 "2025-08-25T07:09:07Z")

</div>

1. It only works for mutable objects.
2. You used `deepcopy()`, but in many case you don’t need to make copies of all attributes (they can even be uncopiable) or you only need to make a level 1 copy of an attribute. This cannot be generalized. `deepcopy()` is too rought tool.
3. `replace()` usually does not allow setting arbitrary attributes.
4. Constructor can have side effect, and you want this side effect happens with values specified as the keyword arguments of `replace()` rather than with original values.

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hlovatt/32/36838_2.png) [@hlovatt](https://discuss.python.org/u/hlovatt)\
**Post date:** [August 25, 2025, 8:18pm UTC](https://discuss.python.org/t/generalize-replace-function/28511/27 "2025-08-25T20:18:07Z")

</div>

@storchaka Yes to all your points, but that doesn’t prevent a useful default from being provided. The documentation could list all your points as reasons to override.

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [August 25, 2025, 8:32pm UTC](https://discuss.python.org/t/generalize-replace-function/28511/28 "2025-08-25T20:32:43Z")

</div>

You could just use dataclasses if you want useful defaults for most things.

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hlovatt/32/36838_2.png) [@hlovatt](https://discuss.python.org/u/hlovatt)\
**Post date:** [August 26, 2025, 12:06am UTC](https://discuss.python.org/t/generalize-replace-function/28511/29 "2025-08-26T00:06:16Z")

</div>

@NeilGirdhar Yes, I use dataclasses a lot and pydantic, but there are still times when a normal class is a good choice.

[Previous page](https://discuss.python.org/t/generalize-replace-function/28511.md?page=1)
