Hey i was wondering about next behaviour
If we have lets say dataclass with None values by default and we want to re-set a value for future instances of this class, the current behaiour of __post_init__
does not take to the account changed static value and pass to the signature loaded first in memory described values of the class.
@dataclass
class A:
attr1: str = None
attr2: str = None
def __post_init__(self):
if not A.attr2:
self.attr2 = '1234'
def __str__(self):
return f'{self.attr1} - {self.attr2}'
A.attr2 = 'qwe'
a = A(attr1='oneoneone')
print(a)
At first I expected that all instances of A would have
attr2 = 'qwe'
But it does not, because it loaded with None being passed to __post_init__
.
oneoneone - None
If we change default value of attr2
to
@dataclass
class A:
attr1: str = None
attr2: str = 'QWERTY'
We see the initializer works
oneoneone - QWERTY
se3r is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.