Content · Programming · Python

Python dataclasses.replace() Function: Usage and Examples

Create a changed copy of a dataclass with dataclasses.replace().

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Create a changed copy of a dataclass with dataclasses.replace().

What You Will Learn


replace(instance, **changes) calls the dataclass constructor to create a new instance with selected field values changed. It does not modify the original object; init=False fields may have special handling.

copy.replace() also supports dataclasses in newer Python versions.

Step-by-Step Example


The example below makes the behavior visible with small input values that you can change safely.

from dataclasses import dataclass, replace

@dataclass(frozen=True)
class AccountSettings:
    theme: str
    notifications: bool

original_settings = AccountSettings("light", True)
updated_settings = replace(original_settings, theme="dark")
print(original_settings.theme, updated_settings.theme)

How the Example Works


  • The imports provide the standard-library tools used in the demonstration: dataclasses.
  • The class AccountSettings groups data and behavior; its methods run when called on an instance.
  • The variable original_settings holds the result of AccountSettings('light', True). Later lines use that value or inspect how it changes.
  • The variable updated_settings holds the result of replace(original_settings, theme='dark'). Later lines use that value or inspect how it changes.

For the chosen input, the expression original_settings.theme, updated_settings.theme prints light dark. Compare that result with the rule above before you change the argument.

Expected output from the example:

light dark

Another Example: Compare the Results


Try a second input and compare it with the first. Notice whether the result changes in type, value, order, or number of items.

from dataclasses import dataclass, replace
@dataclass
class Item:
    name: str
    amount: int
old = Item("Paper", 2)
new = replace(old, amount=5)
print(old.amount, new.amount)

Expected output:

2 5

When You Would Use This


These tools are useful when composing small reusable functions, adapting callbacks, or eliminating repeated work. Pay attention to whether the operation returns a new object, updates state, or produces lazy results. Test a small success case and a boundary case before integrating it into an application.

Practice: Modify the Code


Update one field of a frozen dataclass while keeping the original.

Verify the changed output against the behavior described above, including the relevant invalid or boundary case.

For complete parameters and version details, consult the official Python documentation.

Common Beginner Mistakes


  • Expecting replace() to mutate the original object in place.
  • Trying to replace a nonexistent field or an InitVar incorrectly.

Key Takeaways


  • Create a changed copy of a dataclass with dataclasses.replace().
  • replace(instance, **changes) calls the dataclass constructor to create a new instance with selected field values changed.
  • copy.replace() also supports dataclasses in newer Python versions.
Share this resourceSend this page to someone who may find it useful.