如何解决使用用户定义的类键入提示?
的,如果arg
接受以下 :
def FuncA(arg: CustomClass):
# ^ instance of 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
。相反,带有 ——具体来说,它将接受 的。
该文档包含一个带有int
该类的示例:
a = 3 # Has type 'int' b = int # Has type 'Type[int]' c = type(a) # Also has type 'Type[int]'
解决方法
似乎找不到明确的答案。我想为一个函数做一个类型提示,类型是我定义的一些自定义类,称为它CustomClass()
。
然后让我们说在某个函数中,调用它FuncA(arg)
,我有一个名为 的参数arg
。输入提示的正确方法FuncA
是:
def FuncA(arg: CustomClass):
或者会是:
from typing import Type
def FuncA(Arg:Type[CustomClass]):
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。