First time using dataclass, also not really good at Python. The following behaviour conflicts with my understanding so far:
from dataclasses import dataclass
@dataclass
class X:
x: int = 1
y: int = 2
@dataclass
class Y:
c1: X = X(3, 4)
c2: X = X(5, 6)
n1 = Y()
n2 = Y()
print(id(n1.c1))
print(id(n2.c1))
n1.c1.x = 99999
print(n2)
This prints
140459664164272
140459664164272
Y(c1=X(x=99999, y=4), c2=X(x=5, y=6))
Why does c1 behave like a class variable? What can I do to keep n2.c1 != n1.c1, do I need to write an init function?
I can get sensible results with this addition to Y:
def __init__(self):
self.c1 = X(3, 4)
self.c2 = X(5, 6)
prints:
140173334359840
140173335445072
Y(c1=X(x=3, y=4), c2=X(x=5, y=6))