1

(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 ;))))

vartec
  • 131,205
  • 36
  • 218
  • 244
user771524
  • 11
  • 1
  • 2
    If you want staticness, use a static language. Python is completely dynamic, this is one of the consequences. However, you shouldn't throw the towel because of this. I mean, countless people manage to write working Python code and they're at least as if not more productive than people working in static languages. Just give it time to get used to the advantages, propably you'll find they outweight (or **at least** break even with) the features dynamicness costs you. –  May 26 '11 at 15:18
  • If those two functions are in a Class. foe() should be called with self.foe(). If you have set up the PyDev plugin. It should be marked as an error. – Jon Nylander May 26 '11 at 15:22

5 Answers5

7

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

Screenshot of the OP's code parsed by PyLint in PyDev

vartec
  • 131,205
  • 36
  • 218
  • 244
2

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.

sje397
  • 41,293
  • 8
  • 87
  • 103
2

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.

tkerwin
  • 9,559
  • 1
  • 31
  • 47
0

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

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    It's never compiled at all, yes, there exist JIT versions of Python (the main CPython implementation is a JIT too), but there are no guarantees in the language for this. – orlp May 26 '11 at 15:11
  • @nightcracker: You managed to get two important facts wrong in 1.5 lines, congrats! (1) CPython doesn't feature a JIT compiler, it's a plain old bytecode interpreter (as in, it interprets bytecode it compiled Python source code into). (2) Pretty much *all* implementations compile before starting to run. The Python language **requires** at least some analysis before runtime, e.g. to figure out whether a name refers to a local or a global name - `print x; x = 0` is semantically different from `print x` without an assignment to `x` in the same scope. –  May 26 '11 at 15:25
0

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

Community
  • 1
  • 1
Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112