假设您有一个从泛型[T]继承的Python类.有没有办法获取从类/实例中传入的实际类型?
例如
from typing import TypeVar, Type
T = TypeVar('T')
class Test(Generic[T]):
def hello(self):
my_type = T # this is wrong!
print( "I am {0}".format(my_type) )
Test[int]().hello() # should print "I am int"
在here上,建议类型arg出现在该类型的args字段中.事实上,
print( str( Test[int].__args__ ) )
将打印(<;class'int'>;,).然而,我似乎无法直接从实例中访问它,例如替换
my_type = self.__class__.__args__ # this is also wrong (None)
这似乎不对劲.
谢谢