I want to do something like the following.
I want to define a function func(), as:
def func(**kwargs):
if 'a' in kwargs:
a = funcA()
if 'b' in kwargs:
b = funcB()
...
or
def func(**kwargs):
if 'a' in kwargs:
kwargs['a'] = funcA()
if 'b' in kwargs:
kwargs['b'] = funcB()
...
where funcA() and funcB() are defined elsewhere. I want to then call the function func() with a variable number of arguments:
>>> func(a = x)
>>> func(b = y)
>>> func(a = x, b = y)
...
These calls should assign values to x, y, etc. What is the best way to accomplish this?
a = funcA(a)would allow you to do, butfunc(funcA)allows you to pass in function references as parameters.aandbpositionally, sofuncwouldn't be able to tell which one is intended asaand which one is intended asb. Python's execution model doesn't work the way you're expecting. You'll need to get used to return values and the parameter passing mechanics.python3orpython2? 3 let's you do keyword-only arguments more easily and expressively.