[Python-Dev] new draft of PEP 227

Tim Peters tim.one@home.com
Tue, 19 Dec 2000 00:58:45 -0500


[Tim]
> I expect it would do less harm to introduce a compile-time warning for
> locals that are never referenced (such as the "a" in "set").

[Guido]
> Another warning that would be quite useful (and trap similar cases)
> would be "local variable used before set".

Java elevated that last one to a compile-time error, via its "definite
assignment" rules:  you not only have to make sure a local is bound before
reference, you have to make it *obvious* to the compiler that it's bound
before reference.  I think this is a Good Thing, because with intense
training, people can learn to think like a compiler too <wink>.

Seriously, in several of the cases where gcc warned about "maybe used before
set" in the Python implementation, the warnings were bogus but it was
non-trivial to deduce that.  Such code is very brittle under modification,
and the definite assignment rules make that path to error a non-starter.

Example:

def f(N):
    if N > 0:
        for i in range(N):
            if i == 0:
                j = 42
            else:
                f2(i)
    elif N <= 0:
        j = 24
    return j

It's a Crime Against Humanity to make the code reader *deduce* that j is
always bound by the time "return" is executed.