views:

86

answers:

2
#!/usr/bin/python  
str = "this"  
if(1):  
  print "Hi"  
else:  
  print str.any_random_function()  

This doesn't fail when I run the program. I tried py_compile but that didn't indicate the error in the 'else' loop either. Now how can I compile the program and detect errors reliably in python code?

Thanks.

+4  A: 

I think your best bet would be pylint.

Geo
+2  A: 

Python is a dynamic language, so you can't simply check for compiling errors like in static languages (C/C++/Java). If you assign str.any_random_function, the above code would be correct (okay that's a bad example...).

I'd suggest you to use PyDev for Eclipse which automatically finds many common problems in your code, like missing functions/modules etc. It also supports pylint (optional).

AndiDog
It's easy for a random keystore to add a character to a function call. So is it suggested to run pylint through the entire codebase or have unit tests and 100% code coverage to catch that wrong function call?Thanks for replies.
stacka
It takes some time to get used to pylint, and it may produce lots of (useless) messages if not configured correctly. Anyway, as a good software engineer you should always target 100% code coverage using unit tests. The advantage is that they can be run (semi-)automatically - pylint messages must be interpreted by humans.
AndiDog
I would strongly suggest unit tests as well.
Antoine P.