0

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.

7
  • 2
    A function func(*args) won't be able to determine and return the name arg2! Commented Jun 9, 2017 at 7:52
  • Is the p key of arg3 missing ? Commented Jun 9, 2017 at 7:55
  • @Tbaki it's not missing, arg3 is dictionary so we have to go deeper and find out if there are numeric or string elements in arg3. Commented Jun 9, 2017 at 8:02
  • @deceze there is locals() function where names of arguments could be determined, am I wrong? Commented Jun 9, 2017 at 8:03
  • No, locals won't help you. Only a stack trace would, but please for the love of all that is holy don't go there. Commented Jun 9, 2017 at 8:04

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

4 Comments

"and then it would be simple" – For special definitions of "simple"… ಠ_ಠ
I am new in python, so don't blame me. What if as *args could be given only dictionaries?
read about update method of class dict: tutorialspoint.com/python/dictionary_update.htm
That isn't what I've been focused on. Maurice gave simple solution.
0

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}

2 Comments

That's pretty close. One more thing i would like to be there is the element with key 'p' unpacked. Variables with same names should be in in myDict only if their values are the same, if that's not the case then neither of them should be there.
This should be done before you pass the args to the function ... I mean, when you create the arg-variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.