Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring function as return type in Python 3.7 [duplicate]

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.

like image 809
MartenCatcher Avatar asked Jan 23 '26 12:01

MartenCatcher


1 Answers

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
like image 110
Michael Avatar answered Jan 26 '26 01:01

Michael