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--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...
随机推荐
- charles抓包--手机端
Fiddler和charles都是抓包工具,可以抓到pc端的请求,手机上设置代理后也可以抓到手机上的请求,也可以修改请求数据和返回的数据. 在接口已经使用的时候,比如说已经用到了app上,app端测试 ...
- Django的CBV
CBV CBV(class base views) 就是在视图里使用类处理请求. Python是一个面向对象的编程语言,如果只用函数来开发,有很多面向对象的优点就错失了(继承.封装.多态).所以Dja ...
- 【数据处理】OneHotEncoder编码
原创博文,转载请注明出处! # OneHotEncoder编码 OneHotEncoder编码称为"哑编码"或"独热编码",是将表示分类的数据扩维度, ...
- Adobe Flash Builder 4.6 Premium 安装破解
1.到Adobe官网下载FlashBuilder 4.6,有简体中文版: 语言:简体中文(Simplified)http://trials3.adobe.com/AdobeProducts/FLBR/ ...
- manacher 算法 这个人确实写得太好了;
O(n)回文子串(Manacher)算法 资料来源网络 参见:http://www.felix021.com/blog/read.php?2040 问题描述: 输入一个字符串,求出其中最大的回文子串. ...
- Unity的 NavMeshObstacle 的使用详解
这个组件放在场景中的导航网格上,可以阻挡NavMeshAgent的移动.以Unity4.3.4为例. NavMeshObstacle 分为两种模式,一种是普通模式,通过 ...
- Flask第三篇——安装Flask
现在我们来安装Flask: Windows系统安装Flask 如果你的系统是Windows,那安装起来非常方便——pip install flask Mac系统安装Flask Mac安装Flask一般 ...
- WPF自定义组件,自定义属性(依赖属性)
正常定义的以来属性,在XAML里设置值得时候是不触发Setter的,只能在code中用. 监听PropertyChangedCallback事件可以感知XAML里的设置,这样才能code和XAML配合 ...
- 实例-QPSK的fpga实现
- php mysql apache 的字符集
在使用中常常遇到utf-8和utf8,现在终于弄明白他们的使用不同之处了,现在来和大家分享一下,下面我们看一下utf8 和 UTF-8 有什么区别 “UTF-8”是标准写法,php在Windows下边 ...