【发布时间】:2021-08-19 00:45:08
【问题描述】:
我正在学习数据类,但我对 sort_index 的用途及其实际工作方式感到困惑。
我似乎找不到任何有价值的信息。 Python 官方文档没有提到它,这令人难以置信。
这是一个例子:
@dataclass(order=True)
class Person:
sort_index: int = field(init=False, repr=False)
name: str
age: int
weight: int = 190
def __post_init__(self):
self.sort_index = self.weight
那么,sort_index 的目的是什么?这有什么用途?什么时候用?
再次感谢您花时间回答我的问题。我是 Python 新手。
【问题讨论】:
-
官方 Python 文档没有提到它,原因与它没有提到
name、age或weight的原因相同。它是一个用户定义的字段。您需要查看Person类的文档,而不是dataclass的文档。
标签: python python-3.x python-dataclasses data-class