0

all.

I was wondering if it was possible to set multiple keywords at once (via list?) in a function call.

For example, if you do:

foo, bar = 1, 2
print(foo, bar)

The output is (1,2).

For the function

def printer(foo, bar)
    print(foo,bar)

Is it possible to do something like:

printer([foo, bar] = [1,2])

where both keywords are being set with a list?

In particular, the reason why I ask is because I have a function that returns two variables, scale and offset:

def scaleOffset(...):
    'stuff happens here

    return [scale, offset]

I would like to pass both of these variables to a different function that accepts them as keywords, perhaps as a nested call.

def secondFunction(scale=None, offset=None):
    'more stuff

So far I haven't found a way of doing a call like this:

secondFunction([scale,offset] = scaleOffset())
smci
  • 32,567
  • 20
  • 113
  • 146
dgikmo
  • 37
  • 1
  • 5
  • See this answer: http://stackoverflow.com/a/36908/239078 – bjudson Oct 18 '16 at 21:32
  • It may be worth noting that in Python 2, `print` is a statement, not a function. `print(foo, bar)` may be misleading, since the parentheses are creating a tuple, not passing arguments to a function. In Python 3, `print` is a function and you can use things like `*args` syntax. You can get the Python 3 sematics by putting `from __future__ import print_function` at the top of your file (before any other line that's not also a `from __future__ import ...` statement). – Blckknght Oct 18 '16 at 21:38

1 Answers1

3

To pass args as a list

arg_list = ["foo", "bar"]
my_func(*arg_list)

To pass kwargs, use a dictionary

kwarg_dict = {"keyword": "value"}    
my_func(**kwarg_dict)
sytech
  • 29,298
  • 3
  • 45
  • 86