Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of execution and style of coding in Python

Tags:

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.

like image 320
Jason Avatar asked Jun 06 '10 17:06

Jason


People also ask

What is the order of procedure in Python?

A function is called Higher Order Function if it contains other functions as a parameter or returns a function as an output i.e, the functions that operate with another function are known as Higher order Functions.

What is order of execution in programming?

Software has an order of execution. This is the program sequence, meaning the order in which your lines of code will be executed. Your key takeaways are: Software executes synchronously unless asynchronous features are used. Execution is in order until it encounters a control statement.

How does Python code execute?

Python code is translated into intermediate code, which has to be executed by a virtual machine, known as the PVM, the Python Virtual Machine. This is a similar approach to the one taken by Java. There is even a way of translating Python programs into Java byte code for the Java Virtual Machine (JVM).

Which method executed first in Python?

In python, the code runs top-down. Each statement is being calculated on the go. That's why you cannot reference to a class above its definition, for example.


1 Answers

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

like image 200
JAL Avatar answered Sep 21 '22 03:09

JAL