質問

I have a decent sized code in python which I changed a bit and now I see that when executing the code, the script does not bail on language errors like missing function definitions. I did not think it was possible to continue running a script with missing function definition. I used a function in one place with the intention of actually defining the function before running but forgot about it and ran the code. To my surprise I just got the following line printed to the console --

Worker instance has no attribute update_parse_status

Where Worker is the class I was supposed to add the update_parse_status() call to.

I realized that I had a try and a generic catch all exception handler around the code in question like this --

try:
    Worker.update_parse_status()
except Exception as e:
    print e

So python just throws a AttributeError and I was unknowingly catching it. It taught me a valuable lesson to not have catch all exception handlers. Also coming from a compiled language is there a better way to handle this? I mean, can I some how make sure python just exits on language errors? It will be very helpful to atleast weed out the obvious bugs (though I understand that bad code got me into this situation).

役に立ちましたか?

解決

In python all names are looked up at runtime. Therefor, what you refer to as "language" errors are no different than runtime errors.

This makes python different than many other languages. One of the advantages of that fact is that you can easily customize the way names are looked up (e.g. by overriding class's __getattr__) for example.

You can make use of analyzers (e.g. pyflakes is pretty cool, but there are many others) to detect some of the errors before running your program, but no tool would be able to detect all.

他のヒント

You already answered your own question.

  • Don't catch exceptions if you don't need too ...
  • except Exception is dangerous
  • if you must catch the exception but use some logging mechanism to track it down.

Finally, make sure python just exits on errors: don't catch exceptions where it is not needed to guarantee the flow of the program.

Python, being an interpreter, does this. The best way to counter this is unit testing. The unittest module can help here, and setuptools can run these tests for you. You can package these with your source distribution, as well.

Of course, as you discovered, never use the catch-all exception. That also masks errors that unit tests could catch. You can also run your code under a debugger when developing it so this can be tracked more easily as well. Using the catch-all also makes debugging harder (or even impossible).

You can also do static analysys, using tools such as pylint or pyflakes.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top