python3 -> 函数注释 Function Annotations
来源:互联网 发布:小米秒杀软件 编辑:程序博客网 时间:2024/05/17 23:16
Python 3.X新增加了一个特性(Feature),叫作函数注释 Function Annotations
它的用途虽然不是语法级别的硬性要求,但是顾名思义,它可做为函数额外的注释来用。
Python中普通的函数定义如下:
def func(a, b, c): return a + b + c>>> func(1, 2, 3)6
添加了函数注释的函数会变成如下形式:
def func(a: 'spam', b: (1, 10), c: float) -> int: return a + b + c>>> func(1, 2, 3)6
注释的一般规则是参数名后跟一个冒号(:),然后再跟一个expression,这个expression可以是任何形式。
返回值的形式是 -> int,annotation可被保存为函数的attributes。
查看所有的annotation,可通过如下语句:
>>> func.__annotations__{'c': <class 'float'>, 'a': 'spam', 'b': (1, 10), 'return': <class 'int'>}
如果为函数增加了注释,可不可以继续使用默认参数呢?答案是肯定的。
>>> def func(a: 'spam' = 4, b: (1, 10) = 5, c: float = 6) -> int:... return a + b + c... >>> func(1, 2, 3)6>>> func()15>>> func(1, c=10)16>>> func.__annotations__{'c': <class 'float'>, 'a': 'spam', 'b': (1, 10), 'return': <class 'int'>}
参考文献
Learning Python 5th Edition
0 0
- python3 -> 函数注释 Function Annotations
- PEP3107 函数注解 Function Annotations
- Function Annotations
- Python3学习(7)--函数【Function】
- JUnit4 Annotations注释
- tapestry注释介绍Annotations
- tapestry注释介绍Annotations
- tapestry注释介绍Annotations
- Python3 注释
- Python3 注释
- JDK1.5 注释语法(Annotations)
- java 注释 : annotations 元数据
- JDK1.5 注释语法(Annotations)新鲜试用
- JDK1.5 注释语法(Annotations)新鲜试用
- JDK1.5 注释语法(Annotations)新鲜试用
- JDK1.5 注释语法(Annotations)新鲜试用
- JDK1.5 注释语法(Annotations)使用方法
- Nullability Annotations 为空性的注释
- HDU 1800
- 固定数组NSArray 和 可变NSMutableArray
- 服务端(接口,后台)开发规范
- SQL Server2008数据还原(备份集中的数据库备份与现有的数据库不同(报错3154))
- vim 的使用文档
- python3 -> 函数注释 Function Annotations
- 暑期个人赛--第六场--D(包含手写邻接链表模板~!!)
- IO流遍历某个目录下的所有文件
- IOS应用程序多语言本地化解决方案
- MVC自定义HtmlHelper扩展方法.
- DecimalFormat
- 前端神器-sublime插件安装及使用
- 如果我闭上眼睛就是天黑
- 阻塞和非阻塞