14
#!/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?

3
  • 4
    I don't think it's possible to do that in python. Methods can be dinamically added, the exception captured or the getattr method overloaded. In any of those cases, your error will totally depend or runtime information. Commented Jan 22, 2010 at 13:54
  • Actually, this particular example should be caught by pylint. Commented Jan 22, 2010 at 14:05
  • 1
    It doesn't fail because it's not wrong. Your module could be imported in a context where any_random_function is properly defined for the built-in string class. Commented Jan 22, 2010 at 14:09

3 Answers 3

9

I think your best bet would be pylint.

Sign up to request clarification or add additional context in comments.

Comments

4

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).

2 Comments

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.
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.
0

result_str="";
for row in range(0,7):
for column in range(0,7):
if (column == 1 or column == 5 or (row == 2 and (column == 2 or column == 4)) or (row == 3 and column == 3)):
result_str=result_str+"* "
else:
result_str=result_str+" "
result_str=result_str+"\n"
print(result_str);

4 Comments

Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
Also, because Python is indentation-sensitive, you need to fix the formatting of your post, by using this info stackoverflow.com/editing-help
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Hello, please don't just submit code in your answer(s), add some details as to why you think this is the optimal solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.