This article explores a couple of many Python flaws that can let developers incur in very subtle bugs that can be difficult and expensive to uncover. One is the lack of scope for variable declarations. The other is the lack of compiler support to detect shadow declarations. This gives a brief insight into the eternal debate of highly-dynamic interpreted languages versus strongly-typed compiled languages.

The problem

While reviewing some piece of code written in Python, I came across a very interesting bug. The code goes something like this:

Did you see the problem? The if statement is testing item[i] instead of item[row], which is the actual index variable in the immediately enclosing for loop. Very easy to incur, especially when programming under
pressure or time constraints.

Variable i was declared in the list comprehension statement at the beginning of the function. However, because Python lacks variable scope, the programmer was able to use i much further below. Needless to say, the
application exhibited some strange behavior under certain circumstances.

The problem here is that the statement:

ifitem[i]==values:

is really equivalent to:

ifitem[9]==values:

because the list comprehension iterated from 0 to 9, i.e. xrange(10).

Lack of scope

There are many other things to criticize in this code, but this particular issue speaks to a much larger flaw in Python, the lack of scope.

Compiled languages provide consistent scope support for variable declarations, in particular local variables. In the example above, variable i was declared and used inside a list comprehension iteration.

Had Python supported local scope, the interpreter could have flagged the error in the if statement with a NameError exception. This could have saved the application from having erratic behavior and many hours of debugging, which are not cheap.

Strongly-typed compiled languages like C++ do provide support for scope. For example:

Notice how the last line points out how variable value has lost its type and original intention after leaving the inner scope of the for loop.

Shadow declarations

This phenomenon is known as shadow declarations. They are easy to encounter, especially by less experienced programmers and/or in code that has very long function bodies. It’s aggravated by the lack of compiler support to detect
them.

For example, in C++, a programmer can rely on the -Wshadow option to detect such problems ahead of time. In stricter development environments, -Werror can be used to prevent such code from even going through the linking phase.

Conclusion

For some, the highly-dynamic nature of Python is something to praise and brag about. Sure, it has some benefits, to which I will dedicate other articles. However, when such features get in the way and let programmers incur in subtle bugs like this, it really speaks to the lack of more fundamental and better support either by the language itself or the tool set.