Python 3.x introduced the annotation function

Python 3.x introduced annotation function, a function to enhance the annotations, the following is a common custom function:

  1.  
    def dog(name, age, species):
  2.  
    return (name, age, species)

Added note custom function:

  1.  
    DEF Dog (name: STR, Age: ( . 1, 99), Species: 'dog breeds') -> tuple:
  2.  
    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:

  1.  
    dog.__annotations__
  2.  
     
  3.  
    {# 'Age': ( . 1, 99), 'name': STR, 'return': tuple, 'Species': 'dog breed'}

In addition, using the comments function does not affect the use of default parameters:

  1.  
    DEF Dog (name: STR = 'Dobi', Age: ( . 1, 99) = . 3, Species: 'dog breed' = 'Labrador') -> tuple:
  2.  
    return (name, age, species)

operation result:

    1.  
      dog()
    2.  
       
    3.  
      # ( 'Age', 3, 'Labrador')

Guess you like

Origin www.cnblogs.com/cheyunhua/p/11277101.html