在看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
