Python 3.x introduced annotation function, a function to enhance the annotations, the following is a common custom function:
-
def dog(name, age, species):
-
return (name, age, species)
Added note custom function:
-
DEF Dog (name: STR, Age: ( . 1, 99), Species: 'dog breeds') -> tuple:
-
return (name, age, species)
Above, may be used :
to annotate the parameters one by one, annotation content may be any form, such as the type of the parameter, the role of the range and the like, the return value ->
annotation, all comments are saved to the property function.
View these comments can be customized special function of property __annotations__
acquisition, the results of the meeting in the form of a dictionary Returns:
-
dog.__annotations__
-
-
{# 'Age': ( . 1, 99), 'name': STR, 'return': tuple, 'Species': 'dog breed'}
In addition, using the comments function does not affect the use of default parameters:
-
DEF Dog (name: STR = 'Dobi', Age: ( . 1, 99) = . 3, Species: 'dog breed' = 'Labrador') -> tuple:
-
return (name, age, species)
operation result:
-
dog()
-
-
# ( 'Age', 3, 'Labrador')