stevereds
stevereds

Reputation: 389

How to call a function with n parameters, with n set dynamically?

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"...

Upvotes: 0

Views: 72

Answers (3)

SingleNegationElimination
SingleNegationElimination

Reputation: 156158

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)

Upvotes: 2

Yossi
Yossi

Reputation: 12100

Use the following syntax:

gtk.ListStore(*[str] * len(menu))

Upvotes: 0

HennyH
HennyH

Reputation: 7944

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'>

Upvotes: 1

Related Questions