Differences between Protocol.__call__ vs Callable?

89 Views Asked by At

I have a function

def run_thing(cb: Callback) -> Result:
  w: Widget = make_widget()
  res: Result = cb(w)
  return res

I can imagine two ways to define the Callback type.

# Option 1: Callable
# https://docs.python.org/3/library/typing.html#annotating-callables
Callback = typing.Callable[[Widget],Result]
# Option 2: Protocol
# https://docs.python.org/3/library/typing.html#typing.Protocol
class Callback(typing.Protocol):
  def __call__(self,w: Widget) -> Result:
    ...

What are the nuances to consider in the decision between the two above type definitions?

0

There are 0 best solutions below