似乎找不到一个明确的答案。我想为一个函数进行类型提示,类型是我定义的自定义类,称之为CustomClass()
。
然后假设在某个函数中,称之为FuncA(arg)
,我有一个名为arg
的参数。那么正确的方式来为FuncA
进行类型提示是:
def FuncA(arg: CustomClass):
还是会是这样吗:
from typing import Type
def FuncA(Arg:Type[CustomClass]):
似乎找不到一个明确的答案。我想为一个函数进行类型提示,类型是我定义的自定义类,称之为CustomClass()
。
然后假设在某个函数中,称之为FuncA(arg)
,我有一个名为arg
的参数。那么正确的方式来为FuncA
进行类型提示是:
def FuncA(arg: CustomClass):
还是会是这样吗:
from typing import Type
def FuncA(Arg:Type[CustomClass]):
如果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
就像在有关Typing的文档中所写的那样:
文档中包含了一个与
<b>class typing.Type(Generic[CT_co])</b>
A variable annotated with
C
may accept a value of typeC
. In contrast, a variable annotated withType[C]
may accept values that are classes themselves - specifically, it will accept the class object ofC
.
int
类有关的示例:
a = 3 # Has type 'int'
b = int # Has type 'Type[int]'
c = type(a) # Also has type 'Type[int]'
Willem Van Onsem的回答当然是正确的,但我想提供一个小更新。在PEP 585中,标准集合引入了泛型类型提示。例如,以前我们必须这样说:
from typing import Dict
foo: Dict[str, str] = { "bar": "baz" }
现在我们可以放弃typing
模块中的并行类型层次结构,直接说
foo: dict[str, str] = { "bar": "baz" }
此功能在Python 3.9+中可用,如果使用from __future__ import annotations
,也可在3.7+中使用。
对于这个具体问题,这意味着我们现在可以使用内置的type
来注释类,而不是使用from typing import Type
。
def FuncA(arg: type[CustomClass]):
def foo(bar: 'Qux')
和def foo(bar: Qux)
是等价的,只是前者不需要立即加载类型。 - Willem Van Onsem