如果我试图定义一个函数,该函数可以接受其类类型从特定基类继承的任何对象,那么如何使用python中的类型模块来定义它。
我已经看到了此处显示的示例:In python type-hinting, how can I make an argument accept any subclass of a base class?,但是接受的答案是特定的。我想要一个普通的。
例如
import typing
class A:
def __init__(self):
pass
class B(A):
def __init__(self):
super(B, self).__init__()
class C:
# typing.InstanceBase was used as an example method
def __init__(self, object: typing.InstanceBase[A]):
self.object = object
a = A()
b = B()
>> c1 = C(a) # Would not complain about typing
>> c2 = C(b) # Would not complain about typing