[Tutor] jumping from function to function
spir
denis.spir at free.fr
Fri Jan 30 18:21:49 CET 2009
Le Sat, 31 Jan 2009 00:30:18 +0800,
David <ldl08 at gmx.net> a écrit :
> Dear List,
>
> the following comes from Harrington's "Hands-on Python" (section
> 1.11.8): http://www.cs.luc.edu/~anh/python/hands-on/
>
>
> <code>
>
> '''Avoiding any error by passing a parameter'''
>
> def main():
> x = 3
> f(x)
>
> def f(whatever):
> print whatever
>
> main()
>
> </code>
>
> I am not quite sure what is going on here. Could you please correct my
> line of thought?
>
> 1) main() calls the function def main():
>
> 2) in function def main(): the actual parameter 3 is given to the
> function call f().
>
> 3) f() then "jumps" out of function def main(): to call function
> def f(whatever):, whose new value, 3, gets printed.
>
> Is this what is going on? I find it difficult to get my head around this
> one.
> Also: does Python go back to the function call main()'s position after
> step 3)? How does that work? Again via def main():???
Your interpretation is correct. I guess what may trouble here is a question of order. main is defined before f, while its own definition uses f's definition. This is a bit weird, indeed, and does not help reading. But python doesn't mind. Instead, if ever main would actually be called/executed before f's definition, then you would get an error (try it).
denis
------
la vida e estranya
More information about the Tutor
mailing list