Skip to content
Advertisement

Type annotation for partial functions

we have multiple partial-like functions with same type annotation with args and kwargs like:

JavaScript

can I somehow create a template for functions apple, orange, banana and assign it to them? I thought about Protocol with __call__ definition, but it is unclear how to assign it to functions

Advertisement

Answer

Rather than a function, you can use functools.partial:

JavaScript

Another possibility would be to refactor fruit to take a name, and return a function that closes over the fruit name.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement