I want to use type hints in Python, but there are certain cases where the type of my parameters are not clear to me... do functions have a type, or are they a special case. I would like to do something like the following:
pseudocode:
def run_function(function_to_run: fn):
function_to_run(data)
is this possible? If so, what type should I use?
edit: I was hoping to distinguish a function from other callables such as classes... but maybe that doesn't matter and is overly cautious.
as mentioned in the documentation: you can use Callable
.
you could for example use it this way:
from typing import Callable
def run_function(function_to_run: Callable[[int], str]):
function_to_run(data)
if function_to_run
is a function from int
to str
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments