设为首页 收藏本站
查看: 617|回复: 0

[经验分享] python开发_function annotations

[复制链接]

尚未签到

发表于 2015-4-23 06:12:00 | 显示全部楼层 |阅读模式
  在看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': , 'return': 'Nothing to see here', 'ham': 42}
Arguments: wonderful spam
  初略的看了一下,没有理解其参数的涵义,就照着写了一遍程序:



1 def f(ham: 42, eggs: int = 'spam') -> 'Nothing to see here':
2     print('Annotations:', f.__annotations__)
3     print('Arguments:', ham, eggs)
4
5 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': , 'ham': 42, 'return': 'Nothing to see here'}
Arguments: wonderful spam
>>>
  运行效果和python的API中描述的一样。
  搜索了一些资料发现了参数的涵义:
  我们先来看看几个demo:



ONE : 这里给ham赋一个初始值'Hongten'


1 #这里给ham赋一个初始值'Hongten'
2 def f(ham: 42 = 'Hongten', eggs: int = 'spam') -> 'Nothing to see here':
3     print('Annotations:', f.__annotations__)
4     print('Arguments:', ham, eggs)
5
6 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': , 'ham': 42, 'return': 'Nothing to see here'}
Arguments: Hongten spam
>>>


//TWO :  这里把42变为:'这里是ham的注释'


1 #这里把42变为:'这里是ham的注释'
2 def f(ham: '这里是ham的注释' = 'Hongten', eggs: int = 'spam') -> 'Nothing to see here':
3     print('Annotations:', f.__annotations__)
4     print('Arguments:', ham, eggs)
5
6 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': , 'return': 'Nothing to see here', 'ham': '这里是ham的注释'}
Arguments: Hongten spam
>>>


//THREE :  这里把int变为str


1 #这里把int变为str
2 def f(ham: '这里是ham的注释' = 'Hongten', eggs: str = 'spam') -> 'Nothing to see here':
3     print('Annotations:', f.__annotations__)
4     print('Arguments:', ham, eggs)
5
6 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': , '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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59707-1-1.html 上篇帖子: python网络编程学习笔记(8):XML生成与解析(DOM、ElementTree) 下篇帖子: python enumerate 用法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表