Reputation: 37018
You can use function annotations in python 3 to indicate the types of the parameters and return value, like so:
def myfunction(name: str, age: int) -> str:
return name + str(age) #usefulfunction
But what if you were writing a function that expects a function as a parameter, or returns one?
I realize that you can write any valid expression in for the annotations, so I could write "function" as a string, but is that the best/only way of doing it? Is there nothing like the built-in types int/float/str/list/dict
etc? I'm aware of callable
, but I'm wondering if there's anything else.
Upvotes: 7
Views: 913
Reputation: 2349
Very interesting... I've never even heard of function annotations in Python 3. The following code in the interpreter suggests you might use function
in place of str
and int
. Just my two cents.
>>> a = lambda x: x*2
>>> type(a)
<class 'function'>
Upvotes: 4
Reputation: 33387
There's no style defined for annotations. Either use callable
, types.FunctionType
or a string.
PS: callable
was not available in Python 3.0 and 3.1
Upvotes: 8