【发布时间】:2021-03-29 16:54:33
【问题描述】:
我有两个类,A 和 B,它们都有返回另一个实例的方法。当我不使用类型提示时,这种情况可以正常工作,但是,我无法合并类型提示,如下例所示:
class A:
def return_B_with_out_annotation(self):
return B()
def return_B_with_annotation(self) -> B():
return B()
class B:
pass
执行上述代码(在 Python 3.8 中)会产生以下错误:
NameError: name 'B' is not defined
所以我的问题是,为什么A.return_B_with_out_annotation 方法是有效代码,而A.return_B_with_annotation 不是?
【问题讨论】:
标签: python scope annotations type-hinting