【发布时间】:2021-07-22 16:34:23
【问题描述】:
让我们来:
class A:
pass
class A2(A):
@property
def f(self):
return None
class B:
def __init__(el: A)
self._a = el
class B2(B):
def __init__(el: A2)
super().__init__(el)
def m():
self._a.f()
我现在在调用self._a.f() 时在最后一行出现打字错误,说“无法访问类型 A 的成员 f”,即使它是在 B 中的 A2 处声明的,并且它具有该成员。
声明此 sn-p 以使打字工作的正确方法是什么?
【问题讨论】:
-
您在第一行
class A之后忘记了:。 -
为什么在 A 中将
f()定义为@property而在 B2 中它是一个函数?这真是令人困惑 -
我将更新代码以消除混淆。 F 只是一个占位符名称
-
因为
self._a被推断为A的实例
标签: python type-hinting python-typing