Understanding argument unpacking in python

Viewed 53

This is more of a curiosity rather than a problem. Playing around with double pointers I ended up with this chunk of code.

def mimic(trigger, **some_dict):
    print(trigger)
    print(some_dict)

mydict = {}
(look, dict2) = ('copy me', {'some':20})
mydict['args2'] = {'triggers': look}
mimic(**mydict['args2'])

Its output is:

copy me
{}

Now I am confused on why the variable trigger from mimic() argument takes its value from the dictionary mydict which looks like:

{'args2': {'trigger': 'copy me'}}

If I change one of the triggers naming, I get an error that my calling of mimic() needs 1 more argument. This is the way I expected for it to be behaving in the first place, wanting 2 arguments. This code:

def mimic(trigger, **some_dict):
    print(trigger)
    print(some_dict)

mydict = {}
(look, dict2) = ('copy me', {'some':20})
mydict['args2'] = {'triggerB': look}  #Changed here added a 'B'
mimic(1, **mydict['args2'])

Prints this:

1
{'triggerB': 'copy me'}

So now I notice that before the dictionary inside mydict['args2'] has been taken out. Why this happens? I am working in VScode with python 3.9.13. Is it going to happen with any other IDE too?

0 Answers
Related