I used field(init= False) to disable initializing self.ref. It is then a value in post. The following code raises AttributeError: 'Data' object has no attribute 'ref'
from dataclasses import dataclass, field
def make_list(): return [[0] for k in range(9)]
@dataclass
class Data:
rows: list
cols: list
blocks: list
ref: dict = field(init=False)
def __init__(self, slots=None):
self.rows = make_list()
self.cols = make_list()
self.blocks = make_list()
if slots:
for i in range(9):
for j in range(9):
self.cols[j][i] = self.rows[i][j] = slots[i][j]
def __post_init__(self):
print("post-init executed")
self.ref = {"rows": self.rows, "cols": self.cols, "blocks": self.blocks}
test = Data()
print(test)
I am using python 3.8. The code is tested in both pycharm/jupyter. (Same error)
Edit: after correcting the typo: __post__init__
to __post_init__
, I am still getting the error.