Generarl programming question.

Chris Angelico rosuav at gmail.com
Sat Apr 11 17:15:06 CEST 2015


On Sun, Apr 12, 2015 at 1:00 AM,  <jonas.thornvall at gmail.com> wrote:
> If two functions crossreference eachother back and forth what happen with the local variables.
>
> Will there be a new instance of function holding the variables or do they get messed up?

You mean if one function calls another, and that function calls the
first? That's called "mutual recursion":

def func1(x):
    if x % 2: return x + 1
    return func2(x - 2)

def func2(x):
    if x % 3: return x + 2
    return func1(x - 3)

The 'x' inside each function is completely separate, no matter how
many times they get called. They're usually stored on something called
a "call stack" - you put another sheet of paper on top of the stack
every time you call a function, local variables are all written on
that paper, and when you return from a function, you discard the top
sheet and see what's underneath.

For more information, search the web for the key terms in the above
description, particularly the ones I put in quotes.

If this isn't what you're talking about, the best way to clarify your
question is probably to post a simple (even stupidly trivial, like the
one above) example, and ask a question about that code. Someone'll
doubtless help out!

ChrisA



More information about the Python-list mailing list