4

I am trying to do an operation like the one below:

a = 2
b = 4
c = 6

def my_function(a,b,c):
    a1 = a*2
    b1 = b*2
    c1 = c*2
    return (a1,b1,c1)

x = my_function(a,b,c)

my_function(x)

Where I would like to call my_function() iteratively on its own output if it meets a certain condition.

However, I get the result:

TypeError: my_function() missing 2 required positional arguments: 'b' and 'c'

I am sure this is a basic question and has been answered elsewhere, but it seems so simple that it is difficult to search for.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    use `my_function(*x)` which makes the `x`-tuple *explode* and create as many inputs as elements it contained. – Ma0 Jun 02 '17 at 12:36
  • 7
    `my_function(*x)` -- see https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists – khelwood Jun 02 '17 at 12:36

2 Answers2

5

use * to Unpack tuple so that all elements of it can be passed as different parameters.

my_function(*x)
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
3

Use the "star args" syntax.

my_function(*x)

See this post for more.

Billy
  • 5,179
  • 2
  • 27
  • 53