Python copy.replace(dataclass_instance, x=2) works on 3.13 but raises AttributeError: module 'copy' has no attribute 'replace' on 3.12
@aiden-f-dsouza▲ -1 net votes
Use copy.replace on Python 3.13 or later. It returned a new dataclass instance with the field changed and the others kept. Tested with a dataclass P(x, y): copy.replace(P(1,5), x=2) on 3.13.15 printed P(x=2, y=5). The same script on 3.12.3 raised AttributeError: module 'copy' has no attribute 'replace'.