开发者

Python builds ok even with typos in the code?

(note: I'm new to Python)

I had a typo in a function call, and the project built, but crashed at runtime, when hitting the function call with the typo.

Is this normal?! Shouldn't the build have failed?! Am I setup correctly?!

I'm using Eclipse on Linux Centos.

Eclipse does detect other errors (indentation, undefined variables (i.e. if foo:, with foo never declared before causes a build error, etc.)

here is how my stuff looks like:

def foo(self):
   pass

def bar(self):
   foe() 

-> foe instead of foo -> does NOT generate a compilation error, but (of course) crashes at run-time

I can understand that Python sometimes does not know the types a variables before run-time, but can't it detect it when I make a call to a function that开发者_开发技巧 does not even exist within the class??!!

I hope there is very wrong with my setup, otherwise, I'm afraid I will miss Java, C, and all my others statically typed languages ;))))


Actually PyLint will detect that, so if you're using Eclipse with PyDev plug-in, it will mark line with foe() as error.

PyDev can currently find:

  • Undefined variables
  • Undefined variable from import
  • Unused variables
  • Unused imports
  • Unused wild imports
  • Duplicated signatures
  • Import redefinition
  • Unresolved imports
  • No 'self' token declared in a class method
  • Mixing indentation with tabs and spaces
  • Bad indentation (incorrect number of spaces when indenting).

Python builds ok even with typos in the code?


No it can't detect that.

It is dynamic and interpreted. You could actually add functions to classes at runtime - or import modules - so it can't easily detect if the function exists or not.


Python is not "built", in the same way as C is. Functions can be created on the fly in Python. Think of def foo(): as adding an entry foo in a table of functions. When you call a function, Python looks up that function name in the table. If it's not there, you get a runtime error. This is by design. You'll still get error messages, although they will be when the unknown function is actually called.


Python does not compile until you run the program. So it's hard to talk about "compile-time".


You must use third party tool to check so called compilation errors. Look at this question (and answers) and PyChecker or PyLint.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜