0

Let's say I have the python function f(x, y) and a tuple t = (a, b). To apply f to t, I can write f(t[0], t[1]) which is not elegant, especially if the size of the tuple is greater than 2.

f is not my function and I don't want to manage any customisation of the module it comes from. So a solution as f(t) where f is modified is not acceptable.

Is there a way to do something like f(t.elements()) ?

lalebarde
  • 1,684
  • 1
  • 21
  • 36
  • 1
    Have you tried star unpacking `f(*t)`? – Jab May 01 '20 at 15:40
  • In case that the `t` has more than 2 elements, the function will raise an error. So you should define the function like `f(a, b, *args)` then you will get what you want. – Perfect May 01 '20 at 15:45

1 Answers1

2

Try f(*t)

def f(x,y):
    print(x,y)

t = ('Hello','World')

f(*t) #prints out Hello world

* unpack the values for you

umbreon29
  • 223
  • 2
  • 8