Thought: local declarations

Alex Martelli aleaxit at yahoo.com
Tue May 29 11:48:54 EDT 2001


<scleary at jerviswebb.com> wrote in message
news:mailman.991145445.31685.python-list at python.org...
    ...
> I don't really like declarations myself -- but I'd like to be able to flag
> misspellings as an error, though, and I can't think of any other way to do

http://pychecker.sourceforge.net/


Consider a typical Python script marred by a variable-name
mis-spelling such as you fear:

D:\py21>type witherror.py
def myfunc(N):
    temp = 0L
    for i in range(N):
        if i%7: temp = temp+1
        if i%17: remp = temp+1
    return temp

if __name__=='__main__':
    print myfunc(1000)


D:\py21>python witherror.py
857

Pretty hard to spot -- one 'remp' instead of 'temp' and
the result is peculiar.  But... here it comes to save the day:

D:\py21>python pychecker-0.4\checker.py witherror.py
Processing witherror...

Warnings...

d:\py21\witherror.py:1 No module doc string
d:\py21\witherror.py:5 Local variable (remp) not used

D:\py21>

The 'local variable not used' gets a vast majority of
variable-name misspellings.  As a bonus, you are also
warned when you forget docstrings:-).

All you need, then, is to ensure checker.py does get
run (once) any time a module is imported for the first
time after editing it (and/or any time a script is run
for the first time after editing it, I guess, but that
leaves less of a visible trace, so, importing may do).

Fortunately, you can (e.g. in your sitecustomize.py) do
such things as overriding __builtin__.__import__ with
your own version that does that.  A SMOP, but it does
not require language changes.

Presumably, the Python compiler COULD be tweaked to
allow such user-defined checks to happen automatically
in typical circumstances (when a script is run or
a module imported for the first time -- meaning,
first time since last modification -- etc).  Again
a SMOP, a harder one that should no doubt be done
only _after_ pure-Python experimentation and various
prototyping, but, still a SMOP:-).

I would strongly support specific suggestions about
enhancing our *TOOLS* in such ways.  Changing the
*LANGUAGE* would appear to me to have to be a last,
desperate resort if and when all tools-based ideas
had proved unfruitful...


Alex






More information about the Python-list mailing list