Python: typed method arguments
In Python3, you can add a type to function arguments:
def foo(bar: str = "default") -> str:
"""
@param bar: a textual value
"""
return "test"
Now I have two questions. First, how can you do this for a callback function ? Sense, how to determine the signature of this callback in the function header?
def foo(callback) -> str:
"""
@param callback: function(value: str) -> str
"""
# calculate some intermediate stuff
my_var = ...
return callback(my_var)
Secondly, how to do this for tuples . This will include determining that the value is of the tuple type and should have two values (without a triple, etc.).
def foo(value) -> str:
"""
@param value: tuple of strings
"""
v1, v2 = value
return v1 + v2
Thanks for your comments and answers.
To indicate the signature of your callback function, you can use Callable , for example:
from typing import Callable
def foo(callback: Callable[[str], str]) -> str:
# calculate some intermediate stuff
my_var = '...'
return callback(my_var)
tuple :
from typing import Tuple
def foo(value: Tuple[str, str]) -> str:
v1, v2 = value
return v1 + v2