block scope?
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue Apr 10 17:53:57 EDT 2007
Paul Rubin a écrit :
> aleax at mac.com (Alex Martelli) writes:
>
>>>>>locals['x']=5
>>
>>Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>>TypeError: 'builtin_function_or_method' object does not support item
>>assignment
>
>
>
> Whoops, yeah, meant "locals()['x'] = 5".
>
>
>>I think that ideally there should be a runtime error when assigning an
>>item of locals() with a key that's not a local variable name (possibly
>>excepting functions containing exec, which are kind of screwy anyway).
>
>
> I have no opinion of this, locals() has always seemed like a crazy
> part of the language to me and I never use it. I'd be happy to see it
> gone since it makes compiling a lot easier.
I personally find locals() handy in few cases, like
def output():
foo = 42
bar = baaz()
quux = blah(foo, bar)
return "the %(bar)s is %(foo)d and the %(quux)s shines" % locals()
or:
class Foo(object):
@apply
def bar():
def fget(self):
return self._quux / 42
def fset(self, value):
self._quux = value * 42
return property(**locals())
I'd be very unhappy to see it gone...
More information about the Python-list
mailing list