0

有没有办法将相同的参数 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)

像这样的东西,但“自动”......

4

3 回答 3

2

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)
于 2013-05-16T12:27:38.413 回答
1
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'>
于 2013-05-16T12:25:31.480 回答
0

Use the following syntax:

gtk.ListStore(*[str] * len(menu))
于 2013-05-16T12:27:15.790 回答