The order of definitions is simply “everything has to be defined before you call it”. That’s pretty much it.
edit (to include answer in comments, elucidated):
The reason something like
def call_a():
a()
def a():
pass
call_a()
works when you’ve got a()
in call_a()
before a
is even defined as a function is because Python only actually looks up values for symbols on an as-needed basis. When call_a
is evaluated, the a()
call is basically stored as bytecode instructions to “look up what a
is and call it” when the time comes, which isn’t until you get down to the actual invocation of call_a()
at the bottom.
Here is what the disassembled bytecode of call_a
looks like (via dis.dis
):
Disassembly of call_a:
2 0 LOAD_GLOBAL 0 (a)
3 CALL_FUNCTION 0
6 POP_TOP
7 LOAD_CONST 0 (None)
10 RETURN_VALUE
So basically, when you hit call_a
, it loads whatever is stored as a
onto the stack, calls it as a function, and then pops the return value off before returning None
, which is what implicitly happens for anything that doesn’t explicitly return (call_a() is None
returns True
)