Order of execution and style of coding in Python

I am new to Python so please don't flame me if the question is too basic :)

I have read that Python is executed from top - to - bottom.

If this is the case, why do programs go like this:

def func2(): 
    pass

def func1():
    func2()

def func():
    func1()

if __name__ == '__main__':
    func()

So from what I have seen, the main function goes at last and the other functions are stacked on top of it.

Am I wrong in saying this? If no, why isn't the main function or the function definitions written from top to bottom?

EDIT: I am asking why I can't do this:

if __name__ == '__main__':
    func()

def func1():
    func2()

Isn't this the natural order? You keep on adding stuff at the bottom, since it is executed from top to bottom.


The defs are just creating the functions. No code is executed, other than to parse the syntax and tie functions to those names.

The if is the first place code is actually executed. If you put it first, and call a function before it is defined, the result is a NameError. Therefore, you need to put it after the functions are defined.

Note that this is unlike PHP or JavaScript, where functions are 'hoisted' - any function definitions are processed and parsed before everything else. In PHP and JavaScript, it's perfectly legal to do what you are saying and define functions in the source lower down than where they are called. (One detail in JS is that functions defined like function(){} are hoisted, while functions defined like var func1=function(){}; are not. I don't know how it works with anonymous functions in PHP 5.3 yet).

See, in this, cat() will print correctly, and yip() gives you a NameError because the parser hasn't gotten to the definition of yip() at the time you call it.

def cat():
  print 'meowin, yo'

cat()

yip()

def yip():
  print 'barkin, yall'

meowin, yo
Traceback (most recent call last):
File "cat.py", line 5, in
yip()
NameError: name 'yip' is not defined


Python is executed from top to bottom, but executing a "def" block doesn't immediately execute the contained code. Instead it creates a function object with the given name in the current scope. Consider a Python file much like your example:

def func2():
    print "func2"

def func1():
    func2()

def func():
    func1()

if __name__ == '__main__':
    func()

What happens when this script is executed is as follows:

Firstly, a function object is created and bound to the name "func2" in the global scope. Then a function object is created and bound to the name "func1" in the global scope. Then one called "func". Then the "if" statement is executed, the condition is true and the "func()" statement is executed. At this point "func" is a function object found in the global scope, so it is invoked and its code runs. That code contains the "func1()" statement, which is resolved to a call to the "func1" function, and so on.

If you put the "if" statement at the top then when it executes there would not yet be anything defined with the name "func", so you would get an error. The important thing to recognise is that the "def" statement is itself a statement that is executed. It is not like in some other languages where definitions are a separate sort of declaration with no order of execution.

Also note that so long as the "if __name__..." bit is at the end of the file, it doesn't really matter what order the other declarations are in, since by the time any of them are called all of the "def"s will have already been executed.


Python does, in general, process commands from top to bottom. However, a function call will cause Python to execute that function, and continue downward only after that call has ended.

In your example, the Python interpreter executes the following steps:

  1. Define func2.
  2. Define func1.
  3. Define func.
  4. Process if statement if __name__ == '__main__':.
  5. Call the func function (since the condition is true).
  6. Call the func1 function (because that's what func does).
  7. Call the func2 function (because that's what func1 does).
  8. End, because after finishing the call to func2 it has also finished calling func1 and therefore has finished calling func, which was the final statement in the code.

The if __name__ == "__main__" part goes at the end, because presumably whatever your main function does will need all the other definitions in the script. If there were any other function definitions below the main block, then it would not be possible for them to be used in there, since they haven't been seen by the interpreter at that point.


def statements simply define a function - they don't actually run it until the function is invoked. Hence why they generally come before whatever code uses them - they set the function up to be used in the future.

There's no hard requirement that def statements come before anything else, it's just fairly common to put the __main__ code at the bottom (among other things, it makes it clear what's in that section and what isn't, since anything below the if is part of that section). It also makes sure whatever you want to call from the section has already been def'd.