Alcott
Alcott

Reputation: 18585

an error in function definition won't be detected in Python?

Here is a python module,

#a.py

def bar():
    print x  #x not defined, apparently will result in an error

def foo():
    pass

if __name__ == '__main__':
    foo()

The above module can be run ($ python a.py) without any error. Why? Just because bar is not used in __main__?

But bar's definition is executed, isn't it?

Upvotes: 0

Views: 142

Answers (3)

Ben
Ben

Reputation: 71535

Yes, bar's definition is executed, but the definition doesn't contain an error. It is valid Python to define a function that refers to globals that don't yet exist, so long as they exist when the function is called. Consider this:

def bar():
    print x

x = 10
if __name__ == '__main__':
    bar()

This does not result in an error. And this is only sensible, since even if x exists at the time the function is defined, there is nothing to stop you using del on it later. The point when x needs to be defined is when bar is called (if ever), not when bar is defined.

If Python did work the way you are suggesting, then it would be impossible to define mutually recursive functions without weird hacks like temporarily binding one name to None, then defining both functions.

EDIT: To elaborate on Ignacio's answer to the Alcott's question in the comments, yes syntax errors are caught before the function can be executed, but they're actually caught before it can be defined either.

When Python loads a file, it parses the entire contents into statements and then executes the statements one at a time. A syntax error means it was unable to successfully figure out what statements the file contains, so it can't execute anything. So the error will occur when the file is loaded, which means either when you directly run it with the interpreter, or when you import it.

This pre-processing step is known as "compile time", even though Python is not normally thought of as a compiled language; it is technically compiled to a byte code format, but this is almost entirely uninteresting because the byte code pretty much just directly represents the source code statements.

Upvotes: 4

Greg Hewgill
Greg Hewgill

Reputation: 993951

It's true that the definition of bar is executed when you run the script. However, Python can't determine whether a global variable named x actually exists until the whole script is run.

For example, you could do:

if __name__ == '__main__':
    if random.random() < 0.5:
        x = 5
    foo()

The compiler wouldn't be able to determine at compile time whether x is going to exist or not.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799110

Python resolves name lookups at runtime.

def bar():
  print x

x = 3
bar()

Upvotes: 1

Related Questions