2014-04-22 21 views

回答

5

合成地,尾隨逗號is allowed但並不意味着什麼。這幾乎只是一種文體偏好。我認爲大多數 python程序員會離開它(這也是我會給出的建議),但有些人可能更喜歡它,以便稍後添加更多參數很容易。您也可以keep it in there when calling the function

x = foo(
    arg1=whatever, 
    arg2=something, 
    arg3=blatzimuffin, 
) 

這適用於列表和元組太:

lst = [x, y, z,] 
tup = (x, y, z) 
tup = x, # Don't even need parens for a tuple... 

,如果你想很好地格式化嵌套的東西,這是特別好的,你會與需要大量的默認參數的函數更經常看到這一點:

{ 
    "top": [ 
     "foo", 
     "bar", 
     "baz", 
    ], 
    "bottom": [ 
     "qux", 
    ], 
} 

作爲添加的東西,你只需要添加/編輯1號線的列表,而不是2

+0

時,它的認爲他的某些方法有這個逗號,有些則沒有,似乎他打算這樣做。 – Zen

+1

我一定會離開它。參看[在Python元組中拖尾的逗號。](http://stackoverflow.com/questions/11597901/trailing-comma-in-list-not-invalid-syntax) – fr1tz

+0

@禪 - 這不是很奇怪。不幸的是,所有的時間都不一致......程序員不能免除:-) – mgilson

相關問題