如何打字提示一个函数表示returns一个函数?
How to type hint a function that returns a function?
假设我有以下代码:
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
和 returns 的函数 bool
? f
函数的签名是:
def f(questionable_word: str) -> bool
我在 validator
的 ???
位置放了什么?
validator(blacklist: list=['heck', 'muffins']) -> ???:
typing.Callable
就是你想要的:
validator(blacklist: list=['heck', 'muffins']) -> Callable[[str], bool]:
假设我有以下代码:
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
和 returns 的函数 bool
? f
函数的签名是:
def f(questionable_word: str) -> bool
我在 validator
的 ???
位置放了什么?
validator(blacklist: list=['heck', 'muffins']) -> ???:
typing.Callable
就是你想要的:
validator(blacklist: list=['heck', 'muffins']) -> Callable[[str], bool]: