【问题标题】:Type of method parameter of the same class [duplicate]同一类的方法参数类型[重复]
【发布时间】:2018-11-15 03:16:18
【问题描述】:

我想这样设置参数的类型:

class Tree:
    def __init__(self):
        self.list_of_nodes=[]
    def inser_node (self, node: Tree):
        self.list_of_nodes.append(node)

当我尝试这样做时,会引发错误:NameError: name 'Tree' is not defined

【问题讨论】:

  • 自然,当定义它的方法时,类还没有在命名空间中。您必须使用一种解决方法,例如使用字符串 "Tree" 而不是类引用。

标签: python python-3.x typing


【解决方案1】:

在同一类中,必须将类型放在引号中:

def inser_node (self, node: 'Tree'):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-03
    • 2015-05-01
    • 2012-07-17
    • 2019-12-24
    • 1970-01-01
    • 2017-11-22
    • 2014-10-10
    • 1970-01-01
    相关资源
    最近更新 更多