Generarl programming question.

jonas.thornvall at gmail.com jonas.thornvall at gmail.com
Sat Apr 11 17:36:08 CEST 2015


Den lördag 11 april 2015 kl. 17:26:03 UTC+2 skrev Steven D'Aprano:
> On Sun, 12 Apr 2015 01:00 am, jonas.thornvall at gmail.com wrote:
> 
> > If two functions crossreference eachother back and forth what happen with
> > the local variables.
> 
> Nothing. They are local to the function that creates them.
> 
> 
> > Will there be a new instance of function holding the variables or do they
> > get messed up?
> 
> No to both of those. You have two functions, each with it's own locals.
> 
> 
> def spam():
>     colour = "red"
>     print("Inside spam: colour is:", colour)
>     eggs()
>     print("Inside spam after calling eggs: colour is:", colour)
>     eggs()
> 
> 
> def eggs():
>     colour = "yellow"
>     print("Inside eggs: colour is:", colour)
> 
> 
> Calling spam() gives you this output:
> 
> py> spam()
> Inside spam: colour is: red
> Inside eggs: colour is: yellow
> Inside spam after calling eggs: colour is: red
> Inside eggs: colour is: yellow
> 
> 
> Even if the functions call each other (mutual recursion) each function's
> local variables remain local.
> 
> 
> 
> -- 
> Steven

I don't think it matter butt eggs also calls spam, once more.



More information about the Python-list mailing list