Reputation: 431
Function func(*args, **kwargs)
should return dictionary, and all elements of that dictionary should be numeric or string variables.
If argument of function is dictionary, then function should return dictionary with all elements of argument dictionary, and other arguments.
For example:
arg1 = { 'x': 'X', 'y': 'Y' }
arg2 = 2
arg3 = { 'p': arg1, 'pi': 3.14 }
func(arg2, arg3, arg4=4)
should return this:
{ 'x': 'X', 'y': 'Y', 'arg2': 2, 'pi': 3.14, 'arg4': 4 }
How to do that? Recursion is not desirable.
Upvotes: 0
Views: 1277
Reputation: 18126
You could do something like this - but that solution needs argument numbers ascending (arg1, arg2, ..., argN).
def func(*args, **kwargs):
res = {}
for i, a in enumerate(args):
if isinstance(a, dict):
res.update(a)
else:
res['arg%s' % (i+1)] = a
res.update(kwargs)
return res
arg1 = { 'x': 'X', 'y': 'Y' }
arg2 = 2
arg3 = { 'p': arg1, 'pi': 3.14 }
myDict = func(arg1, arg2, arg3, arg4=4)
print(myDict)
returns:
{'x': 'X', 'y': 'Y', 'arg2': 2, 'p': {'x': 'X', 'y': 'Y'}, 'pi': 3.14, 'arg4': 4}
Upvotes: 0
Reputation: 131
When you pass arg2 to the function, the function does not know that it was named arg2 when you passed it in, it just know its value is 2. You would have to get stack trace to know who called that function, scrape source code to get the name and then it would be simple.
But what if I call it like this:
func(2)
what output do you expect then?
Upvotes: 1