在看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的更多相关文章

  1. 【Machine Learning】Python开发工具:Anaconda+Sublime

    Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...

  2. python开发环境搭建

    虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上python官网下载python运 ...

  3. Python开发工具PyCharm个性化设置(图解)

    Python开发工具PyCharm个性化设置,包括设置默认PyCharm解析器.设置缩进符为制表符.设置IDE皮肤主题等,大家参考使用吧. JetBrains PyCharm Pro 4.5.3 中文 ...

  4. Python黑帽编程1.2 基于VS Code构建Python开发环境

    Python黑帽编程1.2  基于VS Code构建Python开发环境 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...

  5. Eclipse中Python开发环境搭建

    Eclipse中Python开发环境搭建  目 录  1.背景介绍 2.Python安装 3.插件PyDev安装 4.测试Demo演示 一.背景介绍 Eclipse是一款基于Java的可扩展开发平台. ...

  6. Python开发:环境搭建(python3、PyCharm)

    Python开发:环境搭建(python3.PyCharm) python3版本安装 PyCharm使用(完全图解(最新经典))

  7. Python 开发轻量级爬虫08

    Python 开发轻量级爬虫 (imooc总结08--爬虫实例--分析目标) 怎么开发一个爬虫?开发一个爬虫包含哪些步骤呢? 1.确定要抓取得目标,即抓取哪些网站的哪些网页的哪部分数据. 本实例确定抓 ...

  8. Python 开发轻量级爬虫07

    Python 开发轻量级爬虫 (imooc总结07--网页解析器BeautifulSoup) BeautifulSoup下载和安装 使用pip install 安装:在命令行cmd之后输入,pip i ...

  9. Python 开发轻量级爬虫06

    Python 开发轻量级爬虫 (imooc总结06--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...

随机推荐

  1. 2.JSR简介 - JavaEE基础系列

    JSR, Java Specification Request, Java规范请求; 也有的地方翻译为Java规范提案. 在前面的文章 1. Java EE简介 - JavaEE基础系列中, 简要介绍 ...

  2. js之广告弹出自动关闭

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  3. NET MVC 升级到5.1后,View视图中代码报错

    使用nuget将项目中MVC4 升级到MVC5,之后项目还可以正常编译运行, 但View视图中相关的很多代码都报错,比如: 1.@model找不到 2.@Html找不到,本该是System.Web.M ...

  4. softmax与logistic关系

    Softmax回归模型,该模型是logistic回归模型在多分类问题上的推广,在多分类问题中,类标签  可以取两个以上的值. Softmax回归模型对于诸如MNIST手写数字分类等问题是很有用的,该问 ...

  5. mysql 关联左表不存在数据 并 根据身份证计算查找大于65岁以上老人

    --- //查找左边不存在数据,不能用 = '' SELECT m.uid FROM es_members m LEFT JOIN es_user_self_care_assessment u ON ...

  6. "http://127.0.0.1:4723/wd/hub"的解释

    先补充一个内容,就是appium安装时候的环境变量配置,必须要配ANDROID_HOME这个变量,不是“要配置”,是“必须配置”,其他的那些放到系统变量的path里就可以了: # coding: ut ...

  7. Oracle基础查询

    select * from teacher--女性老师编号.姓名select tno,tname,gendar from teacher where gendar='女'--姓韩的女性老师编号.姓名. ...

  8. python: delete the duplicates in a list

    下面有几种做法,  其中3之简洁令人惊讶. 1, >>> t = [1, 2, 3, 1, 2, 5, 6, 7, 8] >>> t [1, 2, 3, 1, 2, ...

  9. Oracle11g的服务

    成功安装Oracle 11g数据库后,你会发现自己电脑运行速度会变慢,配置较低的电脑甚至出现非常卡的状况,通过禁止非必须开启的Oracle服务可以提升电脑的运行速度.那么,具体该怎么做呢.按照win7 ...

  10. 关于angular.extend的用法

    ng中的ng-function中会有些方法,便于我们进行js代码的编写 关于angular.extend(dst, src);通过从src对象复制所有属性到dst来扩展目标对象dst.你可以指定多个s ...