TypeVar, , self.o , Type :
from typing import TypeVar, Type
T = TypeVar('T')
class MyObj:
def __init__(self, o: T) -> None:
self.o = o
def get_obj_class(self) -> Type[T]:
return type(self.o)
def accept_int_class(x: Type[int]) -> None:
pass
i = MyObj(3)
foo = i.get_obj_class()
accept_int_class(foo)
s = MyObj("foo")
bar = s.get_obj_class()
accept_int_class(bar)
, o , Any.
, :
def f(cls: Type[T]) -> T:
return cls()
, - , Pycharm, , mypy , __init__ .
( , T , , , , ).