【发布时间】:2021-12-16 12:18:32
【问题描述】:
我想用 Python 编写一个数据类定义,但不能在声明中引用同一个类。
我主要要实现的是这个嵌套结构的类型化,如下图:
@dataclass
class Category:
title: str
children: [Category] # I can't refer to a "Category"
tree = Category(title='title 1', children=[
Category('title 11', children=[]),
Category('title 12', children=[])
])
【问题讨论】:
标签: python type-hinting python-dataclasses recursive-datastructures