Aaaargh! "global name 'eggz' is not defined"

Robert Kern robert.kern at gmail.com
Thu Oct 29 17:32:17 EDT 2009


On 2009-10-29 15:48 PM, kj wrote:
>
> How can one check that a Python script is lexically correct?
>
> As my Python apps grow in complexity and execution, I'm finding it
> more often the situation in which a program dies after a lengthy
> (i.e. expensive) run because the execution reaches, say, a typo.
> Of course, this typo needs to be fixed, but I'd like to find out
> about it before I waste hours on a run that is bound to fail.  Is
> there any way to do this?  I imagine the answer is no, because
> given Python's scoping rules, the interpreter can't know about
> these things at compile time, but I thought I'd ask.

I like using pyflakes. It catches most of these kinds of typo errors, but is 
much faster than pylint or pychecker. That means I can hook up a key macro to 
run it in my editor so I can use it frequently without hesitation (e.g. in Vim, 
it is my makeprg for Python files).

It doesn't catch other stupid errors, of course. Try your best to write small, 
simple, quick-to-run tests for each piece of functionality that you are working 
on. Test the methods you've just coded independently of the rest of your code 
using that small test before doing full hours-long runs of the whole program. 
Bonus: you now have a suite of unit tests.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list