Answers:
该前者是正确的,如果arg接受一个实例CustomClass:
def FuncA(arg: CustomClass):
# ^ instance of CustomClass
如果您想要类CustomClass本身(或子类型),则应编写:
from typing import Type # you have to import Type
def FuncA(arg: Type[CustomClass]):
# ^ CustomClass (class object) itself
就像在有关打字的文档中写的那样:
class typing.Type(Generic[CT_co])带注释的变量
C可以接受type的值C。相反,带注释Type[C]的变量可以接受本身是类的值 -具体地说,它将接受的类对象C。
该文档包含有关int该类的示例:
a = 3 # Has type 'int' b = int # Has type 'Type[int]' c = type(a) # Also has type 'Type[int]'
def foo(bar: 'Qux')等效于def foo(bar: Qux)它不需要立即加载类型。
Type来自py3.6及更高版本?我刚得到一个NameError。