# replace

``` python
replace(obj, /, **changes)
```

A copy of `obj` with some fields changed, as `copy.replace` in Python 3.13.

An object with its own `__replace__` makes the copy itself. Before Python 3.13, dataclasses have no `__replace__`, so `dataclasses.replace` copies them.

## Parameters

| Name | Type | Description | Default |
|----|----|----|----|
| obj | T | A dataclass, such as any `@pytree` object, or an object with `__replace__`. | *required* |
| \*\*changes | Any | The new values, by field name. | `{}` |

## Returns

| Name | Type | Description |
|------|------|-------------|
|      | T    | The copy.   |

## Raises

| Name | Type | Description |
|----|----|----|
|  | TypeError | If `obj` is a class, or is neither a dataclass nor has `__replace__`, or if `changes` names a field `obj` lacks. |
