I can't figure out what **D means in the following codes:
>>> D = {'say': 5, 'get': 'shrubbery'} >>> '%(say)s => %(get)s' % D '5 => shrubbery' >>> '{say} => {get}'.format(**D) '5 => shrubbery'I googled **kwargs in python and most of the results talk about allowing functions to take an arbitrary number of keyword arguments.
The string.format(**D) here doesn't look like something that would allow the function to take an arbitrary number of keyword arguments because I see that the dictionary type variable D is just one argument. But what does it mean here?
It's called argument unpacking.
**D is used to unpack arguments. Expands the dictionary into a sequence of keyword assignments, so...
'{say} => {get}'.format(**D)becomes...
'{say} => {get}'.format(say = 5, get = shrubbery) The **kwargs trick works because keyword arguments are dictionaries .
I leave you a link if you want to know more about the subject https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists