有沒有辦法將相同的參數(shù)傳遞給函數(shù)n次?例如:if len(menu) == 1: gtk.ListStore(str)elif len(menu) == 2: gtk.ListStore(str, str)elif len(menu) == 3: gtk.ListStore(str, str, str)像這樣的東西,但是“自動的” ...
3 回答

慕碼人8056858
TA貢獻1803條經(jīng)驗 獲得超6個贊
我確定您的意思是:
gtk.ListStore(*menu)
可以將序列放入函數(shù)調(diào)用的位置參數(shù)中。splat必須放在位置參數(shù)的末尾,即:
foo(1, 2, *bar)
還可以,但是你做不到
foo(1, *bar, 2)

慕尼黑的夜晚無繁華
TA貢獻1864條經(jīng)驗 獲得超6個贊
def ListStore(*str_collection): #collect arguments passed into str_collection which is a tuple
for s in str_collection:
print(s)
ListStore("A","B","C")
輸出:
>>>
A
B
C
str_collection 具有類型:
>>>
<type 'tuple'>
添加回答
舉報
0/150
提交
取消