[Tutor] program flow

Jeff Shannon jeff@ccvcorp.com
Wed Jul 2 19:33:02 2003


Payal Rathod wrote:

>Hi,
>Gregor example on "magic" funtions has set me thinking. I am wondering
>how a program flow really is now. Consider a small example.
>
>#!/usr/bin/python
>x = 1
>y = 2
>
>def sum1(a,b):
>	.....
>	return a
>	return b
>
>print "Magic Functins! Hello World!"
> 
>z = sum1(x,y)
>
>
>How does python flow works. It works out 1st 3 lines and then when it
>comes to def sum1(a,b): what happens or it jumps that part and goes
>directly to "Magic Functins! Hello World!" and then to z = sum1(x,y)
>from where it jumps to def sum1(a,b):
>

When it gets to 'def sum(...)', it does just that -- it creates that 
function object, and binds it to the name 'sum'.   It then proceeds to 
the print statement, and finally calls the just-created function object 
and binds the result of that call to the name 'z'.  The key here is that 
'def' is a statement, just like any other; it creates an object and 
binds that object to a name, at the time that the statement is executed.

Jeff Shannon
Technician/Programmer
Credit International