python开辟_function annotations
添加时间:2013-7-27 点击量:
在看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
初略的看了一下,没有懂得其参数的涵义,就照着写了一遍法度:
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: <class int>, 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: <class int>, 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: <class int>, 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: <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的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
初略的看了一下,没有懂得其参数的涵义,就照着写了一遍法度:
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: <class int>, 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: <class int>, 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: <class int>, 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: <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