您的问题是您想使用类型提示,但您希望此类本身能够接受自己类型的参数。
类型提示PEP(0484)解释说,您可以使用类型名称的字符串版本作为前向引用。这个例子的
Tree数据结构听起来与此非常相似
OrgUnit。
例如,这有效:
class OrgUnit(object): def __init__(self, an_org_name: str, its_parent_org_unit: 'OrgUnit' = None ):
在Python 3.7,你将能够激活注释推迟评价用
from__future__ import annotations。这将自动将注释存储为字符串,而不是对其进行评估,因此您可以执行
from __future__ import annotationsclass OrgUnit(object): def __init__(self, an_org_name: str, its_parent_org_unit: OrgUnit= None ): ...
按计划,它将成为Python 4.0中的默认设置。



