On this page
copy.replace
Prev: copy.Error | Up: copy
Rel: copy.copy, copy.deepcopy, copy.Error
Ref: docs.python.org — copy.replace
Tags: #public #python3 #stdlib #copy
Import
import copy
from copy import replace
Signature & Overview
copy.replace(obj, /, **changes)
Return a new object replacing specified fields with new values.
Usage
import copy
from dataclasses import dataclass
@dataclass(frozen=True)
class Pt:
x: int
y: int
assert copy.replace(Pt(1, 2), y=9) == Pt(1, 9)
Gotchas
- Requires Python 3.13+: Generic protocol (
__replace__) working ondataclasses,namedtuples,datetime/date/time, and custom classes.
REPL Inspect
from copy import replace
help(replace)
Linked from
Related notes
- tuts/python3/stdlib_module/copy/copy Linked here; links to this page; shared tags: #copy, #stdlib
- tuts/python3/stdlib_module/copy/deepcopy Linked here; links to this page; shared tags: #copy, #stdlib
- tuts/python3/stdlib_module/copy/Error Linked here; links to this page; shared tags: #copy, #stdlib
- tuts/python3/stdlib_module/copy links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib