在python的类型暗示中有没有一种方法可以说“函数的签名和这个相同”?你知道吗
以下工作正常,但需要额外的时间写出签名:
from typing import Callable
fn_sig = Callable[[int], bool] # can I get rid of this?
def callme(a: int) -> bool:
return a > 1
def do_something(cb: fn_sig):
cb(1)
(我想写点什么):
def do_something(cb: Callable[callme]):
或者
def do_something(cb: callme):
但两者似乎都不成立。(python 3.6.3,mypy 0.570)
首先,您可以从
__annotations__
检索有关函数签名的结构化数据:从这里开始,您可以使用一个函数将其转换为所需的类型。你知道吗
更新:一个粗糙的,可能不是普遍的方法来做到这一点:
相关问题 更多 >
编程相关推荐