The Python interpreter executes the following steps:
- First, it checks the syntax.
- Then it executes the code line by line.
- The code inside a function or class declaration is not executed, but its syntax is checked:
def f(x): return y**2 a = 3 # here both a and f are defined
You can run the preceding program because there are no syntactical errors. You get an error only when you call the function f.
In that case, we speak about a runtime error:
f(2) # error, y is not defined