【发布时间】:2020-03-31 23:24:37
【问题描述】:
我正在尝试用 Python 中的数据类弄脏我的手,我想做的是在我的类中有一个计算字段,并将 sort_index 字段添加到调用中,但也想让它冻结,这样我就不能定义后修改此类的任何属性。以下是我的代码:
from dataclasses import dataclass, field
def _get_year_of_birth(age: int, current_year: int=2019):
return current_year - age
@dataclass(order=True, frozen=True)
class Person():
sort_index: int = field(init=False, repr=False)
name: str
lastname: str
age: int
birthyear: int = field(init=False)
def __post_init__(self):
self.sort_index = self.age
self.birthyear = _get_year_of_birth(self.age)
if __name__ == "__main__":
persons = [
Person(name="Jack", lastname="Ryan", age=35),
Person(name="Jason", lastname="Bourne", age=45),
Person(name="James", lastname="Bond", age=60)
]
sorted_persons = sorted(persons)
for person in sorted_persons:
print(f"{person.name} and {person.age} and year of birth is : {person.birthyear}")
似乎我无法在我的类中设置自定义排序字段,也无法创建任何从其他属性计算的属性,因为我使用的是 freeze 。当我运行上面我得到以下错误:
Traceback (most recent call last):
File "dataclasses_2.py", line 30, in <module>
Person(name="Jack", lastname="Ryan", age=35),
File "<string>", line 5, in __init__
File "dataclasses_2.py", line 23, in __post_init__
self.sort_index = self.age
File "<string>", line 3, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'sort_index'
有没有更好的方法呢?请帮忙
【问题讨论】:
-
参见stackoverflow.com/q/57791679/2750819:虽然答案主要涉及
__init__,但同样适用于__post_init__(如在CPython实现中,内部__init__调用__post_init__)。 -
这个答案stackoverflow.com/a/54119384/8960865也很有帮助:)
标签: python-3.7 python-dataclasses