Creating a local variable scope.
ndbecker2 at gmail.com
Fri Sep 18 13:32:50 CEST 2009
markolopa at gmail.com wrote:
> On Sep 11, 7:36 pm, Johan Grönqvist <johan.gronqv... at gmail.com> wrote:
>> Hi All,
>> I find several places in my code where I would like to have a variable
>> scope that is smaller than the enclosing function/class/module
> This is one of the single major frustrations I have with Python and a
> important source of bugs for me. Here is a typical situation
> for i, j in visited:
> a[i, j] = 1
> for i in range(rows):
> a[i, 0] = 1
> for j in range(columns):
> a[0, i] = 1
> As you see the third loop has a bug (I am actually mixing two logics:
> 1) using i for rows and j for columns 2) using i for the first
> iterator and j for the second). The result is a buggy code that is
> tolerated by Python. In C++ or Perl I don't have this problem. I
> wonder whether other people share this opinion and if we have ever had
> PEPs trying to address that...
I agree. I wish there were a convenient way to switch this 'feature'
on/off. I believe the vast majority of the time I do not want variable
names leaking out into other scopes. OTOH, sometimes it's convenient.
More information about the Python-list