有没有办法将相同的参数 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)
像这样的东西,但“自动”......
有没有办法将相同的参数 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)
像这样的东西,但“自动”......
I'm sure what you mean is:
gtk.ListStore(*menu)
Sequences can be splatted into the positional arguments of a function call. The splat must go at the end of positional arguments, ie:
foo(1, 2, *bar)
is OK, but you can't do
foo(1, *bar, 2)
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'>
Use the following syntax:
gtk.ListStore(*[str] * len(menu))