What is the appropriate type hint to specify that a variable should be a function (the equivalent of a delegate, Func<T>
or Action
in C#)?
Is it also possible to specify the function argument types in a generic fashion as well (for example Func<int, int>
)?
I cannot find any relevant details in the documentation.
Here's how you can add type hints to our function: Add a colon and a data type after each function parameter. Add an arrow ( -> ) and a data type after the function to specify the return data type.
This module supports type hints as specified by PEP 484 and PEP 526. The most fundamental support consists of the types Any , Union , Tuple , Callable , TypeVar , and Generic . For full specification please see PEP 484.
Specify a Variable Type There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.
The type() function is used to get the type of an object. Python type() function syntax is: type(object) type(name, bases, dict)
According to the docs, you can use Callable, imported from typing.
You can view more details in PEP 0484.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With