如何编写返回类型提示,以指示函数返回“Class_”类的实例?
Class_ = TypeVar("Class_", bound=type)
def make_class(class_: Class_) -> Class_:
return class_()
Class_ = TypeVar("Class_", bound=type)
def make_class(class_: Class_) -> Class_:
return class_()
typing
中的Type
。这是你尝试用TypeVar("Class_", bound=type)
定义的泛型实现。Type
来指定输入参数。from typing import Type, TypeVar
T = TypeVar("T")
def make_class(class_: Type[T]) -> T:
return class_()
T
,输入是type[T]
(https://docs.python.org/3/library/typing.html#typing.Type)? - jonrsharpe