首页 > 代码库 > 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基础学习(三)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。