Callable Signatures with typing.Callable
`Callable[[ArgTypes, ...], ReturnType]` specifies that a parameter must be a callable function with matching argument types and return type 📞.
from typing import Callable
def calculate(op: Callable[[int, int], int], a: int, b: int) -> int:
return op(a, b)
add = lambda x, y: x + y
print(calculate(add, 10, 20)) # 30 📞
# 96. Callable Signatures with typing.Callable `Callable[[ArgTypes, ...], ReturnType]` specifies that a parameter must be a callable function with matching argument types and return type 📞. ```python from typing import Callable def calculate(op: Callable[[int, int], int], a: int, b: int) -> int: return op(a, b) add = lambda x, y: x + y print(calculate(add, 10, 20)) # 30 📞 ``` If a function accepts arbitrary arguments of any type, use an ellipsis: `Callable[..., ReturnType]`. Perfect for generic decorator wrappers! 📞 --- ### Question How do you annotate a callback function that accepts a string and returns a boolean? 🧠- [ ] a. `Callable[[str], bool]` 📞 - [ ] b. `Function(str -> bool)` - [ ] c. `Callable(str, bool)` - [ ] d. `Type[str, bool]` --- ### Challenge Import `Callable`. Define `def run_fn(f: Callable[[int], int], val: int) -> int: return f(val)`. Call `run_fn(lambda x: x + 10, 5)` and print result. 🎯 <details> <summary>Solution</summary> ```python from typing import Callable def run_fn(f: Callable[[int], int], val: int) -> int: return f(val) print(run_fn(lambda x: x + 10, 5)) ``` </details>