How to set the value of dataclass field in __post_init__ when frozen=True?

前端 未结 1 426
日久生厌
日久生厌 2021-02-05 00:34

I\'m trying to create a frozen dataclass but I\'m having issues with setting a value from __post_init__. Is there a way to set a field value based on values from an

1条回答
  •  野性不改
    2021-02-05 00:50

    Use the same thing the generated __init__ method does: object.__setattr__.

    def __post_init__(self):
        object.__setattr__(self, 'value', RANKS.index(self.rank) + 1)
    

    0 讨论(0)
提交回复
热议问题