你如何表达一个没有参数的 Python Callable?
How do you express a Python Callable with no arguments?
在 Python typing
包的文档中,says:
It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis for the list of arguments in the type hint: Callable[..., ReturnType].
另一方面,also says:
Callable[..., ReturnType]
(literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType.
我想表达一个不带参数但 returns 字符串的函数。省略号似乎表明有一些未指定的参数。我想表达的是,绝对是零争论。
除了在类型提示中使用 Callable[..., str]
,我还有其他选择吗?
它需要一个参数类型序列,所以如果没有类型,你就给它传递一个空序列:
Callable[[], str]
在 Python typing
包的文档中,says:
It is possible to declare the return type of a callable without specifying the call signature by substituting a literal ellipsis for the list of arguments in the type hint:
Callable[..., ReturnType].
另一方面,also says:
Callable[..., ReturnType]
(literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType.
我想表达一个不带参数但 returns 字符串的函数。省略号似乎表明有一些未指定的参数。我想表达的是,绝对是零争论。
除了在类型提示中使用 Callable[..., str]
,我还有其他选择吗?
它需要一个参数类型序列,所以如果没有类型,你就给它传递一个空序列:
Callable[[], str]