I'm pretty sure, this has been asked before, but I don't know, what to search for..
I want to type hint a "generic" function (like a function template in C++)
e.g. (this example makes no sense at all - it's just for demonstration)
def foo(fn: Callable[Any, Any], args: Any) -> Any:
return fn(args)
I want to use this function in a context where I have full type information, so I hope I can get rid of any Any
.
def bar() -> int:
func: Callable[[str], int] = lambda arg: len(arg)
return foo(func, "Hurz")
One way of course would be to just explicitly turn foo
into (Callable[[str], int], str) -> int
, but I'm looking for the generic approach.
I guess what I'm looking for has something to do with generics, but I can't see how to use them to create "function templates".