1.使用函数参数标注,能提示程序员应该怎样正确使用这个函数
2.以字典的形式存放在函数的 __annotations__ 属性中,并且不会影响函数的任何其他部分
3.形参标注的定义方式是在形参名称后加上冒号,后面跟一个表达式,该表达式为标注的值。
4.返回值标注的定义方式是加上一个组合符号 ->,后面跟一个表达式,该标注位于形参列表和表示 def 语句结束的冒号之间
如:
def adda(x: int, y: int)->str:
return x + y
print(adda.__annotations__) # {'x': <class 'int'>, 'y': <class 'int'>, 'return': <class 'str'>}
print(adda(1, 31)) # 32
print(type(adda(1, 31))) # <class 'int'>