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--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ... 
随机推荐
- Linux下打开windows中文文本乱码问题
			1. 查看文件的编码方式:file命令 $ file test_file.txt test_file.txt: ISO- text, with very long lines $ file train ... 
- python pass关键字神奇吗
			参考文献:http://blog.sina.com.cn/s/blog_76e94d210100vz3e.html 1.空语句 do nothing2.保证格式完整3.保证语义完整 好吧!它什么也没干 ... 
- vue-router(二)后代路由
			关键字:router,children ,router-link,router-view,router-link-active 先理解什么是children? 后代路由为某路由中用到router-vi ... 
- absolute的left和right的妙用
			之前做了一个自定义鼠标右键的布局,做的过程中遇到了一个很有趣的问题,之前一直没有注意到. 目标样式如下: 期初并不知道文字内容需要随机,所以写的时候写“死”了. 所有的内容都是按照设计的四个文字走的, ... 
- BZOJ - 3295 动态逆序对 (树状数组套treap)
			题目链接 思路和bzoj2141差不多,不过这道题的数据更强一些,线段树套treapT了,树状数组套treap卡过~~ #include<bits/stdc++.h> using name ... 
- java 邮件发送工具类【来源网络自己已经实际应用】
			最近在做一个Java发送邮件的工具类,现在分享一下完整的代码 首先需要java邮件的包javax.mail-1.5.4.jar 之前因为链接给错了,很不好意思,现在重新发一次. 包在这里可以下载htt ... 
- 每天一个linux命令(权限):【转载】chown命令
			chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ... 
- Java并发--Thread类详情
			以下是本文的目录大纲: 一.线程的状态 二.上下文切换 三.Thread类中的方法 转载原文链接:http://www.cnblogs.com/dolphin0520/p/3920357.html 一 ... 
- test20190308
			测试 晚上考试,是 \(SCOI\ 2016\ Day\ 2\) 的题目. 妖怪 由于之前在洛谷上用三分水过去了,就很 \(naive\) 地打了一个三分就跑了.获得 \(10\) 分好成绩. 记 \ ... 
- Appium + Python App自动化(2)第一个脚本
			[1]打开你的夜神模拟器(或者连接你的手机) [2]打开桌面的Appium [3]下载你要测的App的apk文件,放到桌面 [4]拖动你的apk安装包到夜神模拟器里,然后模拟器会提示你安装.安装.原来 ... 
