block scope?

James Stroud jstroud at mbi.ucla.edu
Fri Apr 6 23:48:00 EDT 2007


Neal Becker wrote:
> 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



More information about the Python-list mailing list