[Python-3000] locals(), closures, and IronPython...

Georg Brandl g.brandl at gmx.net
Tue Mar 6 19:03:24 CET 2007


Aahz schrieb:
> On Tue, Mar 06, 2007, Andrew Dalke wrote:
>> On 3/5/07, Guido van Rossum <guido at python.org> wrote:
>>>
>>> I don't know too many good use cases for
>>> locals() apart from "learning about the implementation" I think this
>>> might be okay.
>> 
>> Since I'm watching this list for any discussion on the traceback
>> threads, I figured I would point out the most common use I know
>> for locals() is in string interpolation when there are many local
>> variables, eg:
>> 
>>    a = "spam"
>>    b = "egg"
>>     ...
>>    y = "foo"
>>    z = "bar"
>> 
>>   print fmtstr % locals()
> 
> I'll second this one.

While we're at it: I've had a thought about string formatting in Py3k.
Suppose you do something like

name = "He"
what = "Ex-Parrot"
print "{name} is an {what}".format(name=name, what=what)

it seems a bit too verbose. Why not have format() without any arguments
default to format(**locals())?

This would give those Perl-style interpolation supporters something they
can live with, and it would be quite handy for common usage.

Georg



More information about the Python-3000 mailing list