+ 3

What is this expression in python ?

What is this expression in the code ? What does it mean ? https://code.sololearn.com/cIPNZ9NxxwF1/?ref=app

5th Feb 2020, 4:36 PM
WARLORD
WARLORD - avatar
3 Answers
+ 4
That's a so-called annotation, specifying which type is supposed to be passed to the argument. It's basically an optional tool to optically keep track of what's going on, when the programs become larger. It does not do anything directly, but you can access these annotations from the function(animal.__annotations__) and involve the stored information in your code.
5th Feb 2020, 4:50 PM
HonFu
HonFu - avatar
+ 4
HonFu does this mean that passing an integer to animal() raises an error ?
5th Feb 2020, 4:53 PM
WARLORD
WARLORD - avatar
+ 2
No, it doesn't. But you can force this behavior anyway, if you want to, even without annotations, for example by writing: if not isinstance(arg, list): raise TypeError Or something like that.
5th Feb 2020, 5:02 PM
HonFu
HonFu - avatar