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

Joshua Landau joshua.landau.ws at gmail.com
Mon Jun 24 20:13:15 CEST 2013


On 24 June 2013 19:03, Terry Reedy <tjreedy at udel.edu> wrote:
> 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
> ???=.

Oh look! It's Guido's time machine!

"Look, it's {what_you_wanted}!".format_map(locals())

Note that your suggestion would disallow:
"{mapping}".format(mapping="HA")

>> 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 ;-).


More information about the Python-ideas mailing list