首页 > 代码库 > Python基础学习(三)

Python基础学习(三)

"""函数注解是函数定义的可选择元数据部分
注解作为一个字典存储在 __annotations__属性中,不对函数本身产生任何影响、
参数注解的定义是在参数后面加上一个冒号,后跟一个表达式对注释的值进行评估。
返回注释由一个文字->定义,后面是一个在参数列表和表示标记语句结束的冒号之间的表达式。
"""
def f(ham: str, eggs: str=‘eggs‘,)->str:
print("Annotations:", f.__annotations__)
print("Arguments:", ham, eggs)
return ham + ‘ and ‘ + eggs


f(‘spam‘)

运行结果:

D:\Python3.6.1\python.exe F:/python_workspace/tutorial/TestAnnotation.py
Annotations: {‘ham‘: <class ‘str‘>, ‘eggs‘: <class ‘str‘>, ‘return‘: <class ‘str‘>}
Arguments: spam eggs

Process finished with exit code 0

 

https://www.python.org/dev/peps/pep-0484/

https://docs.python.org/3/tutorial/controlflow.html

Python基础学习(三)