[Python-ideas] Short form for keyword arguments and dicts

Terry Reedy tjreedy at udel.edu
Mon Jun 24 20:03:58 CEST 2013


On 6/24/2013 12:23 PM, Andrew McNabb wrote:

> Another use case where this would come in handy is with string
> formatting:
>
>>>> print('{spam} and {eggs}'.format(spam=spam, eggs=eggs))
>
> I've seen people use an awful workaround for this:
>
>>>> print('{spam} and {eggs}'.format(locals()))

That should be
print('{spam} and {eggs}'.format(**locals()))

Why do you see an intended usage as an 'awful workaround'?

If it is the inefficiency of unpacking and repacking a dict, that could 
be fixed. One possibility is for ** to just pass the mapping when the 
function only reads it, as is the case with .format (but how to know?). 
A direct solution for .format is to add a keyword-only mapping
parameter:

print('{spam} and {eggs}'.format(map=locals()))

or mapping= names= or reps= (replacements) or strs= (strings) or dic= or 
???=.

> While it looks a little magical, the proposed syntax would be an
> improvement (especially when there are many arguments):
>
>>>> print('{spam} and {eggs}'.format(=spam, =eggs))

It looks pretty hideous to me ;-).
And it still has repetition ;-).

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list