[Python-ideas] Short form for keyword arguments and dicts
Eric V. Smith
eric at trueblade.com
Mon Jun 24 20:16:26 CEST 2013
On 6/24/2013 2:03 PM, Terry Reedy 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()))
or:
print('{spam} and {eggs}'.format_map(locals()))
which solves the inefficiency problem mentioned below.
> 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()))
Today that would be:
print('{map.spam} and {map.eggs}'.format(map=locals()))
--
Eric.
More information about the Python-ideas
mailing list