【问题标题】:Type hint for argument of type A in method of class A [duplicate]A类方法中A类型参数的类型提示[重复]
【发布时间】:2017-01-03 06:23:50
【问题描述】:

我正在尝试通过以下代码在 Python 3.5.1 中使用类型提示:

>>> class A:                         
...     def a(self, i: int, b: A):   
...         pass                     
...                                  
Traceback (most recent call last):   
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in A       
NameError: name 'A' is not defined   

我猜想在解析类型 A 时确定类型 A 尚未完全构造是问题,但我不明白为什么这样的语法不好。有没有其他方式表达它或者它只是非法构造?

我想在基类中使用这个语法提示,它可以在树状层次结构中组成派生类。

【问题讨论】:

  • 考虑使用装饰器来处理这种类型的东西,或元类

标签: python python-3.x


【解决方案1】:

试试这个:

class A:

    def a(self, i: int, b: 'A'):
        pass

欲了解更多信息,请查看here

【讨论】:

    猜你喜欢
    • 2019-12-24
    • 2016-01-27
    • 2019-07-05
    • 2019-09-30
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多