Return a new object replacing specified fields with new values. This is especially useful for frozen classes. Example usage:: @dataclass(frozen=True) class C: x: int y: int c = C(1, 2) c1 = replace(c, x=3) assert c1.x == 3 and c1.y == 2
(obj, /, **changes)
| 1752 | |
| 1753 | |
| 1754 | def replace(obj, /, **changes): |
| 1755 | """Return a new object replacing specified fields with new values. |
| 1756 | |
| 1757 | This is especially useful for frozen classes. Example usage:: |
| 1758 | |
| 1759 | @dataclass(frozen=True) |
| 1760 | class C: |
| 1761 | x: int |
| 1762 | y: int |
| 1763 | |
| 1764 | c = C(1, 2) |
| 1765 | c1 = replace(c, x=3) |
| 1766 | assert c1.x == 3 and c1.y == 2 |
| 1767 | """ |
| 1768 | if not _is_dataclass_instance(obj): |
| 1769 | raise TypeError("replace() should be called on dataclass instances") |
| 1770 | return _replace(obj, **changes) |
| 1771 | |
| 1772 | |
| 1773 | def _replace(self, /, **changes): |
nothing calls this directly
no test coverage detected