[Tutor] program flow

Rodrigues op73418@mail.telepac.pt
Wed Jul 2 07:32:01 2003


> -----Original Message-----
> From: tutor-admin@python.org
> [mailto:tutor-admin@python.org]On Behalf Of
> Payal Rathod
> Sent: quarta-feira, 2 de Julho de 2003 4:10
> To: Python Tutor ML
> Subject: [Tutor] program flow
>
>
> 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
>

Note that

return b

will never be executed since the function returns before it gets
there! Maybe you want:

return a, 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):
>
> What is the flow here?
>

the def statement is a statement of sorts, but it *is* executed, *not*
jumped over. What happens is that the function body is "compiled" into
a function object and this object is bounded to the name sum1. Then
Python continues to the other statements. The same thing for a class
statement.

The following gives some info on function objects:

>>> def sum1(a, b):
... 	return a
...
>>> print sum1
<function sum1 at 0x010F20A8>
>>> help(sum1)
Help on function sum1 in module __main__:

sum1(a, b)

>>> for elem in dir(sum1):
... 	print elem
...
__call__
__class__
__delattr__
__dict__
__doc__
__get__
__getattribute__
__hash__
__init__
__name__
__new__
__reduce__
__repr__
__setattr__
__str__


> With warm regards,
> -Payal
>

With my best regards,
G. Rodrigues