python开发_function annotations
在看python的API的时候,发现了一个有趣的东东,即:python的方法(函数)注解(Function Annotation)
原文:
4.7.7. Function Annotations
Function annotations are completely optional, arbitrary metadata information about user-defined functions. Neither Python itself nor the standard library use function annotations in any way; this section just shows the syntax. Third-party projects are free to use function annotations for documentation, type checking, and other uses. Annotations are stored in the __annotations__ attribute of the function as a dictionary and have no effect on any other part of the function. Parameter annotations are defined by a colon after the parameter name, followed by an expression evaluating to the value of the annotation. Return annotations are defined by a literal ->, followed by an expression, between the parameter list and the colon denoting the end of the def statement. The following example has a positional argument, a keyword argument, and the return value annotated with nonsense:
>>> def f(ham: 42, eggs: int = 'spam') -> "Nothing to see here":
... print("Annotations:", f.__annotations__)
... print("Arguments:", ham, eggs)
...
>>> f('wonderful')
Annotations: {'eggs': <class 'int'>, 'return': 'Nothing to see here', 'ham': 42}
Arguments: wonderful spam
初略的看了一下,没有理解其参数的涵义,就照着写了一遍程序:
def f(ham: 42, eggs: int = 'spam') -> 'Nothing to see here':
print('Annotations:', f.__annotations__)
print('Arguments:', ham, eggs) f('wonderful')
运行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Annotations: {'eggs': <class 'int'>, 'ham': 42, 'return': 'Nothing to see here'}
Arguments: wonderful spam
>>>
运行效果和python的API中描述的一样。
搜索了一些资料发现了参数的涵义:
我们先来看看几个demo:
ONE : 这里给ham赋一个初始值'Hongten'
#这里给ham赋一个初始值'Hongten'
def f(ham: 42 = 'Hongten', eggs: int = 'spam') -> 'Nothing to see here':
print('Annotations:', f.__annotations__)
print('Arguments:', ham, eggs) f()
运行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Annotations: {'eggs': <class 'int'>, 'ham': 42, 'return': 'Nothing to see here'}
Arguments: Hongten spam
>>>
//TWO : 这里把42变为:'这里是ham的注释'
#这里把42变为:'这里是ham的注释'
def f(ham: '这里是ham的注释' = 'Hongten', eggs: int = 'spam') -> 'Nothing to see here':
print('Annotations:', f.__annotations__)
print('Arguments:', ham, eggs) f()
运行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Annotations: {'eggs': <class 'int'>, 'return': 'Nothing to see here', 'ham': '这里是ham的注释'}
Arguments: Hongten spam
>>>
//THREE : 这里把int变为str
#这里把int变为str
def f(ham: '这里是ham的注释' = 'Hongten', eggs: str = 'spam') -> 'Nothing to see here':
print('Annotations:', f.__annotations__)
print('Arguments:', ham, eggs) f()
运行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Annotations: {'eggs': <class 'str'>, 'ham': '这里是ham的注释', 'return': 'Nothing to see here'}
Arguments: Hongten spam
>>>
//FOUR : 看看一段java函数代码
/**
* 判断一个字符串是否全为字母,此方法比上面的isAllChar方法效率要高,但是需要的是str中包含非字母字符在靠前面
* 如:"a2bcd",对于这个字符串,到字符"2"的时候就会终止判断
*
* @param str
* 所需判断的字符串
* @return str中是否全为字母字符
*/
public static boolean isAllChars(String str) {
if (str == null || str.equals("")) {
return false;
}
boolean flag = true;
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) < 'a' || str.charAt(i) > 'z')
&& (str.charAt(i) < 'A' || str.charAt(i) > 'Z')) {
flag = false;
break;
}
}
return flag;
}
到这里你大概知道我想说什么了吧!
总结:
def f(ham: 42, eggs: int = 'spam') -> "Nothing to see here":
print("Annotations:", f.__annotations__)
print("Arguments:", ham, eggs) #def关键字定义了函数f,在函数f中有两个参数:ham,eggs。
#其中ham没有默认值,而eggs是由默认值的,其默认值为'spam'.
#参数ham的注释部分为:42;参数eggs的注释部分为:int
# "Nothing to see here"是返回值的注释,这个必须用 '->'连接 #看了java代码后,你会有更直观的认识,注释嘛,你可以根据你自己的想法,想怎么写就怎么写,如42,int;不过不好的注释有时候会给别人阅读代码带来很大的麻烦 #如果上面的代码是这样写:
def f(ham: int = 42, eggs: str = 'spam') -> 'Nothing to see here':
print("Annotations:", f.__annotations__)
print("Arguments:", ham, eggs)
#我想很多人阅读代码的时候就很容易理解啦
#上面只是个人观点,如果不正确的地方,还请大家指正 #同时也希望大家相互学习:hongtenzone@foxmail.com
python开发_function annotations的更多相关文章
- 【Machine Learning】Python开发工具:Anaconda+Sublime
Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...
- python开发环境搭建
虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上python官网下载python运 ...
- Python开发工具PyCharm个性化设置(图解)
Python开发工具PyCharm个性化设置,包括设置默认PyCharm解析器.设置缩进符为制表符.设置IDE皮肤主题等,大家参考使用吧. JetBrains PyCharm Pro 4.5.3 中文 ...
- Python黑帽编程1.2 基于VS Code构建Python开发环境
Python黑帽编程1.2 基于VS Code构建Python开发环境 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...
- Eclipse中Python开发环境搭建
Eclipse中Python开发环境搭建 目 录 1.背景介绍 2.Python安装 3.插件PyDev安装 4.测试Demo演示 一.背景介绍 Eclipse是一款基于Java的可扩展开发平台. ...
- Python开发:环境搭建(python3、PyCharm)
Python开发:环境搭建(python3.PyCharm) python3版本安装 PyCharm使用(完全图解(最新经典))
- Python 开发轻量级爬虫08
Python 开发轻量级爬虫 (imooc总结08--爬虫实例--分析目标) 怎么开发一个爬虫?开发一个爬虫包含哪些步骤呢? 1.确定要抓取得目标,即抓取哪些网站的哪些网页的哪部分数据. 本实例确定抓 ...
- Python 开发轻量级爬虫07
Python 开发轻量级爬虫 (imooc总结07--网页解析器BeautifulSoup) BeautifulSoup下载和安装 使用pip install 安装:在命令行cmd之后输入,pip i ...
- Python 开发轻量级爬虫06
Python 开发轻量级爬虫 (imooc总结06--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...
随机推荐
- Qt jsoncpp 对象拷贝、删除、函数调用 demo
/*************************************************************************************************** ...
- 【排序】希尔排序,C++实现
原创博文,转载请注明出处! 本文代码的github地址 # 基本思路 希尔排序是”直接插入排序“的改进版,也称为“缩小增量排序”.基本原理:先将待排序的数组元素分成多个序列,然后对每个子序 ...
- 【剑指offer】不使用新变量,交换两个变量的值,C++实现
# 题目 不使用新变量,交换两个变量的值. # 思路 方法一:使用加减法操作,交换两个变量的值. A = A+B B = A-B A = A-B 方法二:使用异或运算,交换两个变量的值 A = A^B ...
- Git详解之七 自定义Git
以下内容转载自:http://www.open-open.com/lib/view/open1328070404827.html自定义 Git 到目前为止,我阐述了 Git 基本的运作机制和使用方式, ...
- python 生成器推导式与列表推导式的区别
生成器表达式现用现生成,列表推导式一次性生成静态数据 L = [2, 3, 5, 7] L2 = (x**2+1 for x in L) it = iter(L2) print(next(it)) L ...
- c++学习笔记(5)
1.两个相邻的仅由空格,制表符或者换行符分开的字符串字面值,可连接成一个新的字符串 cout<<"a multi-line " "string literal ...
- 每天一个linux命令:【转载】rm命令
今天学习一下linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除 ...
- 流畅设计 Fluent Design System 中的光照效果 RevealBrush,WPF 也能模拟实现啦!
UWP 才能使用的流畅设计效果好惊艳,写新的 UWP 程序可以做出更漂亮的 UI 啦!然而古老的 WPF 项目也想解解馋怎么办? 于是我动手实现了一个! 迫不及待看效果 ▲ 是不是很像 UWP 中 ...
- Hadoop常见错误问题及解决方法总结二
问题导读:1.遇到问题该如何排除错误?2.看不到namenode的可能原因是什么?3.地址占用该如何解决?4.could only be replicatied to 0 nodes, instead ...
- vector 介绍
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...