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