tags:

views:

76

answers:

2

I do most of my development in Java and C++ but recently had to write various scripts and picked up Python. I run python from the command line on scripts; not in interactive mode. I'm wondering if

I like a lot of things about the language, but one thing that keeps reducing my productivity is the fact that I get no advance warning if I am using a variable that is not yet defined.

For example, somewhere in the code I forget to prefix a variable with its declaring module, or I make a little typo, and the first time I learn about it is when the program crashes.

Is there a way to get the python interpreter to throw advance warnings if something might be funky when I access a variable that hasn't been accessed or set somewhere else in the program? I realize this is somewhat against the philosophy of the language, but I can't be the only one who makes these silly errors and has no way of catching them early.

+2  A: 

there are some tools like pylint or pyflakes which may catch some of those. pyflakes is quite fast, and usable on many projects for this reason

As reported on pyflakes webpage, the two primary categories of defects reported by PyFlakes are:

  • Names which are used but not defined or used before they are defined
  • Names which are redefined without having been used
David Cournapeau
Looks cool. I hope that Eclipe's PyDev is configurable enough for me to add it as a step.
Uri
+2  A: 

Pydev is pretty well integrated with Pylint, see here -- and pylint is a much more powerful checker than pyflakes (beyond the minor issue of misspelled variables, it will catch style violations, etc, etc -- it's highly customizable for whatever your specific purposes are!).

Alex Martelli
Looks good. I'll give it a shot tomorrow. I have to refactor a lot of code and I was dreading it because of the naming issues; this will certainly help. Thank you!
Uri
Although there is no question about pylint being much more complete than pyflakes, the performance of pyflakes makes it a valid choice in some situations, IMO.
David Cournapeau
Guess it depends on what HW you're using for development -- I mostly use a slow Macbook Air, a semi-slow Macbook Pro (both 1st generation), and a Linux workstation that was state-of-the-art when I got it over 4 years ago... on each of those, I find pylint's performance to be hardly an issue (it gets run automatically, with a very picky customized suite of style checks, whenever I request a code review or submit code that's passed code review). That's on a Python project with a few dozen thousands lines -- if you work on much larger projects or slower HW, your mileage may vary;).
Alex Martelli