加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python开发_function annotations

发布时间:2020-12-17 00:18:45 所属栏目:Python 来源:网络整理
导读:在看python的API的时候,发现了一个有趣的东东,即:python的方法(函数)注解(Function Annotation) 原文: 4.7.7. Function Annotations Function annotations are completely optional,arbitrary metadata information about user-defined functions. Neith

在看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  any way; this section just shows the syntax. Third-party projects are free to use function annotations  documentation,type checking,Annotations are stored <span style="color: #0000ff;">in the <span style="color: #800080;">annotations attribute of the function as a dictionary <span style="color: #0000ff;">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 <span style="color: #0000ff;">and the colon denoting the end of the <span style="color: #0000ff;">def statement. The following example has a positional argument,a keyword argument,<span style="color: #0000ff;">and the <span style="color: #0000ff;">return<span style="color: #000000;"> value annotated with nonsense:
>>> <span style="color: #0000ff;">def f(ham: 42,eggs: int = <span style="color: #800000;">'<span style="color: #800000;">spam<span style="color: #800000;">') -> <span style="color: #800000;">"<span style="color: #800000;">Nothing to see here<span style="color: #800000;">"<span style="color: #000000;">:
... <span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">Annotations:<span style="color: #800000;">",f.<span style="color: #800080;">annotations<span style="color: #000000;">)
... <span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">Arguments:<span style="color: #800000;">"<span style="color: #000000;">,ham,eggs)
...
>>> f(<span style="color: #800000;">'<span style="color: #800000;">wonderful<span style="color: #800000;">'<span style="color: #000000;">)
Annotations: {<span style="color: #800000;">'<span style="color: #800000;">eggs<span style="color: #800000;">': <<span style="color: #0000ff;">class <span style="color: #800000;">'<span style="color: #800000;">int<span style="color: #800000;">'>,<span style="color: #800000;">'<span style="color: #800000;">return<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">Nothing to see here<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">ham<span style="color: #800000;">': 42<span style="color: #000000;">}
Arguments: wonderful spam

初略的看了一下,没有理解其参数的涵义,就照着写了一遍程序:

f(ham: 42,eggs: int = ) -> (,f. ( f()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6,May 16 2013,00:03:43) [MSC v.1600 32, >>> ================================ RESTART ================================ >>>: < >,: 42,: >>>

运行效果和python的API中描述的一样。

搜索了一些资料发现了参数的涵义:

我们先来看看几个demo:

f(ham: 42 = ,eggs: int = ) -> (,f. ( f()

运行效果:

Python 3.3.2 (v3.3.2:d047928ae3f6,: >>>
f(ham: = ,: >>>
f(ham: = ,eggs: str = ) -> (, >>> ================================ RESTART ================================ >>>: < >,: ,: >>>
(str == || str.equals("" flag = ( i = 0; i < str.length(); i++ ((str.charAt(i) < 'a' || str.charAt(i) > 'z'&& (str.charAt(i) < 'A' || str.charAt(i) > 'Z'=

到这里你大概知道我想说什么了吧!

总结:

f(ham: 42,eggs: int = ) -> (,f.(<span style="color: #008000;">#<span style="color: #008000;">def关键字定义了函数f,在函数f中有两个参数:ham,eggs。<span style="color: #008000;">

<span style="color: #008000;">其中ham没有默认值,而eggs是由默认值的,其默认值为'spam'.<span style="color: #008000;">

<span style="color: #008000;">参数ham的注释部分为:42;参数eggs的注释部分为:int<span style="color: #008000;">

<span style="color: #008000;"> "Nothing to see here"是返回值的注释,这个必须用 '->'连接

<span style="color: #008000;">#<span style="color: #008000;">看了java代码后,你会有更直观的认识,注释嘛,你可以根据你自己的想法,想怎么写就怎么写,如42,int;不过不好的注释有时候会给别人阅读代码带来很大的麻烦

<span style="color: #008000;">#<span style="color: #008000;">如果上面的代码是这样写:
<span style="color: #0000ff;">def f(ham: int = 42,eggs: str = <span style="color: #800000;">'<span style="color: #800000;">spam<span style="color: #800000;">') -> <span style="color: #800000;">'<span style="color: #800000;">Nothing to see here<span style="color: #800000;">'<span style="color: #000000;">:
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">Annotations:<span style="color: #800000;">",eggs)<span style="color: #008000;">#<span style="color: #008000;">我想很多人阅读代码的时候就很容易理解啦 <span style="color: #008000;">

<span style="color: #008000;">上面只是个人观点,如果不正确的地方,还请大家指正 #同时也希望大家相互学习:hongtenzone@foxmail.com

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读