Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, tuple arguments playing nicely with others

For example:

mytuple = ("Hello","World")
def printstuff(one,two,three):
    print one,two,three

printstuff(mytuple," How are you")

This naturally crashes out with a TypeError because I'm only giving it two arguments when it expects three.

Is there a simple way of effectively 'splitting' a tuple in a tider way than expanding everything? Like:

printstuff(mytuple[0],mytuple[1]," How are you")
like image 689
Bolster Avatar asked Dec 10 '22 09:12

Bolster


1 Answers

Kinda,... you can do this:

>>> def fun(a, b, c):
...     print(a, b, c)
...
>>> fun(*(1, 2), 3)
  File "<stdin>", line 1
SyntaxError: only named arguments may follow *expression
>>> fun(*(1, 2), c=3)
1 2 3

As you can see, you can do what you want pretty much as long as you qualify any argument coming after it with its name.

like image 162
Skurmedel Avatar answered Dec 27 '22 07:12

Skurmedel