具有相同类型属性的类的类型注释?

时间:2020-08-27 15:34:21

标签: python python-3.x oop type-annotation

我有Node类,Node类有2个孩子,孩子A和B,但是它们是具有相同结构的同一类,因为孩子可以有自己的孩子。

代码在python中,并且该语言不允许我做下一个,将我标记为错误:

class Node:
    a: Node
    b: Node
    name: str
    value: int

有没有办法做到这一点,对象本身是嵌套的?

我的问候

1 个答案:

答案 0 :(得分:1)

根据How do I specify that the return type of a method is the same as the class itself?,最简单的解决方案是使用具有类名的字符串:

a : 'Node'