0

Is there a way to pass the same parameter n times to a function?

For example:

if len(menu) == 1:
    gtk.ListStore(str)
elif len(menu) == 2:
    gtk.ListStore(str, str)
elif len(menu) == 3:
    gtk.ListStore(str, str, str)

Something like this, but "automatic"...

stevereds
  • 379
  • 1
  • 2
  • 10

3 Answers3

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)
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
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")

Outputs:

>>> 
A
B
C

str_collection has type:

>>> 
<type 'tuple'>
HennyH
  • 7,794
  • 2
  • 29
  • 39
0

Use the following syntax:

gtk.ListStore(*[str] * len(menu))
Yossi
  • 11,778
  • 2
  • 53
  • 66