python - 如何键入提示返回函数的函数?

标签 python python-3.x type-hinting

<分区>

假设我有以下代码:

def validator(blacklist: list=['heck', 'muffins']):

    def f(questionable_word: str) -> bool:
        return questionable_word in blacklist

    return f

validator_british = validator(['pish'])
validator_british('pish')  # returns True
validator_british('heck')  # returns False

我的问题是如何对 validator 函数进行类型提示,使其指示返回一个函数,特别是一个接受 str 并返回 bool f 函数的签名是:

def f(questionable_word: str) -> bool

validator??? 位置应该放什么?

validator(blacklist: list=['heck', 'muffins']) -> ???:

最佳答案

typing.Callable是你想要的:

validator(blacklist: list=['heck', 'muffins']) -> Callable[[str], bool]:

关于python - 如何键入提示返回函数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42705709/

相关文章:

regex - 根据列中的字符串过滤数据框

python - 在类构造函数中表达兄弟嵌套类的类型提示

python - Pandas 按日期重新采样并选择第二个最小值

python - 更改python shell的背景颜色

python - 使用 Python 托管身份创建 Azure 数据工厂链接服务

python - 尝试将 Python 列表保存到 S3 存储桶时出错。

python - 当我从字典中打印 2 个键和 2 个值时,第一个键和值将自己替换为第二个键和值

Python 类型提示 : how to tell X is a subclass for Foo?

PhpStorm - 参数的 PHPDoc "any"类型

python - 删除 pandas 数据框中以注释字符开头的项目