I recently ran into strange behavior related to dataclasses in python. When using fields with default_factory, repeated call of the decorator results in an error.
As for me, this looks like a bug, because the field does not work out as a full-fledged default value. Accordingly, I would like to ask a question. what is the reason for this behavior of dataclasses?
source:
@dataclass
class A:
a: int = 5
b: int = 6
B = dataclass(A)
print(B == A)
output:
True
on the other hand
@dataclass
class A:
a: int = 5
b: List = field(default_factory=lambda: [1, 2, 3])
B = dataclass(A)
print(B == A)
output:
TypeError: non-default argument 'b' follows default argument
and finally if you change order of a and b in last snippet you'll obtain True in the output.