In python 3.7 I may declare types for functions and methods. I know that it is optional and I may not do it. But Python is not my main language and this feature helps me a lot and makes my code more readable.
But I cannot find a correct type for closures. Could you help me?
def external_function(closure_param: str) -> ???:
def inner_function(param: str) -> bool:
# some code
return inner_function
I was searching for a correct type in built-in packages typing and types but didn't find suitable.
Python has a module called types and its main purpose is to store built-in types that aren't available in builtins.py. FunctionType, the type you are looking to return is in types.py. Now to rewrite your code.
from types import FunctionType
def externalfunction(closure_param: str) -> FunctionType:
def internalfunction(param: str) -> bool:
# some code
pass
return internalfunction
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With