Creating a local variable scope.

markolopa at markolopa at
Fri Sep 18 11:36:17 CEST 2009

On Sep 11, 7:36 pm, Johan Grönqvist <johan.gronqv... at> 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 definition.

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...


More information about the Python-list mailing list