Forward References

Like some other high-level languages, Python does not permit you to reference or call a function before it has been declared. We can try a few examples to illustrate this:

def foo():
    print 'in foo()'
    bar()

If we were to call foo() here, it will fail because bar() has not been declared yet:

>>> foo()
in foo()
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in foo
NameError: bar

We will now define bar(), placing its declaration before foo()’s declaration:

def bar():
    print 'in bar()'
def foo():
    print 'in foo()'
    bar()

Now we can safely call foo() with no problems:

>>> foo()
in foo()
in bar()