10

I have a hello function and it takes n arguments (see below code).

def hello(*args):
  # return values

I want to return multiple values from *args. How to do it? For example:

d, e, f = hello(a, b, c)

SOLUTION:

def hello(*args):
  values = {} # values
  rst = [] # result
  for arg in args:
    rst.append(values[arg])
  return rst

a, b, c = hello('d', 'e', f)
a, b = hello('d', 'f')

Just return list. :) :D

Zeck
  • 6,045
  • 18
  • 67
  • 105
  • No sure what you mean. `return args` will return multiple values from `*args`. – Marcelo Cantos Dec 05 '11 at 06:34
  • Duplicate , answered in detail here. [Stack overflow ret mul val python][1] [1]: http://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python – Ray Garner Dec 05 '11 at 06:35
  • In hello function I'm assigning values to variables. I get that variables from *args. You know args are dynamic. So I don't know return multiple values from dynamic variables /args/ like example. – Zeck Dec 05 '11 at 06:40

4 Answers4

13

So, you want to return a new tuple with the same length as args (i.e. len(args)), and whose values are computed from args[0], args[1], etc. Note that you can't modify 'args' directly, e.g. you can't assign args[0] = xxx, that's illegal and will raise a TypeError: 'tuple' object does not support item assignment. What You need to do then is return a new tuple whose length is the same as len(args). For example, if you want your function to add one to every argument, you can do it like this:

def plus_one(*args):
    return tuple(arg + 1 for arg in args)

Or in a more verbose way:

def plus_one(*args):
    result = []
    for arg in args: result.append(arg + 1)
    return tuple(result)

Then, doing :

d, e, f = plus_one(1, 2, 3)

will return a 3-element tuple whose values are 2, 3 and 4.

The function works with any number of arguments.

Johan Boulé
  • 1,634
  • 11
  • 17
  • You know 'args' arguments are dynamic. How to return multiple values from dynamic 'args'? Sorry for poor english. Please see my comment. – Zeck Dec 05 '11 at 06:46
  • 1
    Woow. Better solution than me. Thank you for sharing your knowledge. :) – Zeck Dec 05 '11 at 08:42
1

Just return a tuple:

def hello(*args):
    return 1, 2, 3

...or...

    return (1, 2, 3)
detly
  • 26,649
  • 13
  • 85
  • 142
1

args is a list. if you return a sequence (list, tuple), Python will try to iterate and assign to your d, e, f variables. so following code is ok.

def hello(*args):
   return args

d, e,  f = hello(1,2,3)

As long as you have, the right number of values in the *args list. It will be assigned to your variables. If not, il will raise a ValueError exception.

d, e, f = hello(1, 2) #raise ValueError

I hope ith helps

luc
  • 37,543
  • 21
  • 117
  • 168
0

Just return them. For instance, if you want to return the parameters unmodified, do this:

def hello(*args):
    return args

If you want to return something else, return that instead:

def hello(*args):
    # ...
    # Compute d, e and f
    # ...
    return d, e, f
Marcelo Cantos
  • 167,268
  • 37
  • 309
  • 353