Search code examples
pythoncompilationideinitializationwing-ide

Finding uninitialized variables before running script


How do I detect whether all variables have been initialized in a Python script before running the script? Ideally, I would like my compiler (currently Wing IDE) to detect errors as part of the build/compilation process. This is to prevent the program from crashing after ~30min of loading data etc. due to a simple mistake.

I know that Python can manipulate variable in all sorts of ways. However, my scripts tend to be fairly simple and I do not expect the method to be bullet-proof.

As an example I'd like the error detection method to find simple errors such as the misspelling of grocery_bill in the script below.

prices = {'apple': 0.40, 'banana': 0.50}
my_purchase = {
    'apple': 1,
    'banana': 6}
grocery_bill = sum(prices[fruit] * my_purchase[fruit]
                   for fruit in my_purchase)
print 'I owe the grocer $%.2f' % grocery_blil

Solution

  • There's a two options I can currently think of.

    1. PyLint

    This tool can analyse all your source code and try to detect places where bugs may be happening. This should detect things like the one you have mentioned.

    1. PyCharm

    This is an IDE by Jetbrains, and you can turn on spell check for all your variables. While this does not stop typos from happening, it does make it far easier to catch them.

    If you're a student, you can get the full version of PyCharm for free.