這是函數(shù)注解,Python 3.x引入,它的特點(diǎn)有
創(chuàng)新互聯(lián)專注于河西網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供河西營(yíng)銷型網(wǎng)站建設(shè),河西網(wǎng)站制作、河西網(wǎng)頁(yè)設(shè)計(jì)、河西網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造河西網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供河西網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
對(duì)函數(shù)的參數(shù)進(jìn)行類型注解,以冒號(hào)標(biāo)記
對(duì)函數(shù)的返回值進(jìn)行類型注解,以箭頭標(biāo)記
只對(duì)函數(shù)參數(shù)或返回值做一個(gè)輔助的說(shuō)明,并不對(duì)函數(shù)參數(shù)或返回值進(jìn)行類型檢查
提供給第三方工具,做代碼分析,發(fā)現(xiàn)隱藏bug
函數(shù)注解的信息,保存在__annotations__屬性中
注解本身是一個(gè)字典類型的數(shù)據(jù)
你的程序我?guī)湍阃晟屏?函數(shù)注解部分的解釋見注釋),你看看吧
from?typing?import?List
def?f(a)?-?List[dict]:?#函數(shù)注解,返回一個(gè)字典列表,但是它不對(duì)返回值類型進(jìn)行檢查
print(a)??#打印字典
return?[a]?#返回字典列表
print(f.__annotations__)?#打印函數(shù)注解
l={'Name':?'Zara','Age':17}?#把字典傳入函數(shù)
print(f(l))?#打印函數(shù)返回值
源代碼(注意源代碼的縮進(jìn))
-:標(biāo)記返回函數(shù)注釋,信息作為.__annotations__屬性提供,__annotations__屬性是字典。鍵return是用于在箭頭后檢索值的鍵。但是在Python中3.5,PEP 484 - Type Hints附加了一個(gè)含義:-用于指示函數(shù)返回的類型。它似乎也將在未來(lái)版本中強(qiáng)制執(zhí)行。
eg:
def test() - [1, 2, 3, 4, 5]:
pass
print(test.__annotations__)
輸出:
{'return': [1, 2, 3, 4, 5]}
你用的graphics模塊?這不是內(nèi)置的,雖然它是調(diào)用內(nèi)置的Tkinter畫圖。
option可以是"first","last","both"或"none"。見graphics.py:
def setArrow(self, option):
if not option in ["first","last","both","none"]:
raise GraphicsError(BAD_OPTION)
self._reconfig("arrow", option)
細(xì)節(jié)要查Tk文檔:
6.6. The canvas line object
In general, a line can consist of any number of segments connected end to end, and each segment can be straight or curved. To create a canvas line object on a canvas C, use:
id = C.create_line ( x0, y0, x1, y1, ..., xn, yn, option, ... )
The line goes through the series of points
(x0,
y0),
(x1,
y1),
…
(xn,
yn).
Options include:
arrow The default is for the line to have no arrowheads. Use
arrow=FIRST to get an arrowhead at the(x0,y0)end of the line. Use
arrow=LAST to get an arrowhead at the far end. Use
arrow=BOTH for arrowheads at both ends.