block scope?

J

John Nagle

Paul said:
(e-mail address removed) (Alex Martelli) writes:
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 think of that, from a compiler perspective, as one of the features
that, if used, means you have to switch to a more inefficient representation.

I encourage the hard-code optimizing compiler people to keep plugging
away on Python. It's a convenient way to program, but the implementations
are slower than they should be a decade into the language's life cycle.

John Nagle
 
A

Alexander Schmolck

Neal Becker said:
One thing I sometimes miss, which is common in some other languages (c++),
is idea of block scope. It would be useful to have variables that did not
outlive their block, primarily to avoid name clashes. This also leads to
more readable code.

I have on occassion used lambda as a poor-man's let, but only if I needed to
avoid multiple evaluation:

res = (lambda x=blah(...), y=blahz(...): f(x*y,x+y))()

I'm sure most people would debate it's more readable, but it's IMO superior to
cleaning up manually with ``del``. I sometimes also find it useful to avoid
cluttering up the interactive shell.

'as
 
P

Paddy

Neal said:
One thing I sometimes miss, which is common in some other languages (c++),
is idea of block scope. It would be useful to have variables that did not
outlive their block, primarily to avoid name clashes. This also leads to
more readable code. I wonder if this has been discussed?

Probably, with good code, block scope would be overkill, except that I
would welcome list comprehensions to have a new scope:

py> i
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
<type 'exceptions.NameError'>: name 'i' is not defined

py> [i for i in xrange(4)]
[0, 1, 2, 3]
py> i # hoping for NameError
3

Yep, i think that we need consistent scope rules for
listexps and genexps. Isn't it coming in 3.0?

If it is, then maybe it will be back-ported to
Python 2.6.

In Python 2.5 we have the following:
[k for k in (j for j in range(5))] [0, 1, 2, 3, 4]
k 4
j
Traceback (most recent call last):

- Paddy.
 
G

Georg Brandl

Alex said:
Paul Rubin said:
exec?
option 1: that just runs the compiler a bit later ...

Besides exec, there's also locals(), i.e.
locals['x'] = 5
can shadow a variable. Any bad results are probably deserved ;)
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

I suspect you want to index the results of calling locals(), rather than
the builtin function itself. However:
... locals()['x'] = 5
... return x
... Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f
NameError: global name 'x' is not defined

No "shadowing", as you see: the compiler knows that x is NOT local,
because it's not assigned to (the indexing of locals() does not count:
the compiler's not expected to detect that), so it's going to look it up
as a global variable (and not find it in this case).

Even assignments to real local variable names in the locals() result do
normally not result in the variable having a new value.
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 would make the locals() result completely independent from the frame,
and document that it is read only.

(though, this needs some other way for trace functions to interact with
the frame's local variables.)

Georg
 
B

Bruno Desthuilliers

Paul Rubin a écrit :
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top