Generarl programming question.
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sat Apr 11 11:25:50 EDT 2015
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
More information about the Python-list
mailing list