Reputation: 2193
Say I have some set of functionlike below:
def bar(b: str, func)->int:
return func(b)
I would like to specify in bar
the input type and return type of func
that get's passed as a parameter. That way I can pass in a different versions of func
that also take a string but return an int.
Something maybe like
def bar(b: str, func(a: str) -> list)->int:
my_list = func(b)
my_output = # do something with my_list that returns an int
return my_output
but this doesn't seem to work. Is this possible in python?
Upvotes: 3
Views: 1323
Reputation: 44444
You can use typing.Callable
:
from typing import Callable
def bar(b: str, func: Callable[[str], int])->int:
return func(b)
Upvotes: 3