5

I have the following recursive function, and I'm having trouble figuring out how python handles variables in recursive functions. Will it create a copy of the addresses variable for every recursion, or will it overwrite the variable and create a horrible mess?

def get_matches():
    addresses = get_addresses()

    #do stuff

    for addr in addresses:
        #do stuff
        if some_condition:
            get_matches()
        else:
            return
Edward Minnix
  • 2,889
  • 1
  • 13
  • 26
jamzsabb
  • 1,125
  • 2
  • 18
  • 40
  • 6
    Different scope, different variable. – erip Oct 05 '17 at 15:50
  • `addresses` is a local variable... – juanpa.arrivillaga Oct 05 '17 at 15:57
  • @erip scope is the magic word I was missing in my Google searches, thanks! – jamzsabb Oct 05 '17 at 15:58
  • 1
    In general, the *name* `addresses` exists in each scope (or frame) of the recursion. However, if `get_addresses()` is defined so that it returns a single mutable *object*, then the *name* doesn't matter because all frames will be referencing the same *object*. We generally don't talk about *variables* in Python, but rather *names* and *objects*. – Jared Goguen Oct 05 '17 at 15:59
  • @jamzsabb Further reading on Python memory allocation if you're interested: https://stackoverflow.com/questions/14546178/does-python-have-a-stack-heap-and-how-is-memory-managed (both answers could be helpful). – Casey Kuball Oct 05 '17 at 19:14
  • 1
    Try you cold here http://pythontutor.com/, then you will see – englealuze Oct 10 '17 at 08:47
  • @englealuze pythontutor is pretty great! The best way to not only ask questions like this but really SEE the answer. Beautiful, thanks for the recommendation! – jamzsabb Oct 11 '17 at 14:06

1 Answers1

5

The underlining concept your looking for is called a frame.

Inside of the Python interpreter is a stack commonly referred to as the call stack. Each time Python encounters a function call during execution, a new frame object is created and pushed on the stack. The frame represents the function call. Each one has it's own scope, and the current value of any arguments passed into the function.

This means that even for each recursive call of a function, a new frame is created for that specific function call and pushed on the stack. As I already said above, each frame has it's own scope. So each frames' scope has an address variable defined in it, separate from any other.

Note however that frame object's themselves do not store the values of the variables. You see, the Python interpreter only operates on the top-most frame of the stack. Python uses another stack, separate from the call stack, to store the values of the local variables of the frame it's currently executing.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • Given the simplicity of the question, would probably be good to mention that most modern programming languages use a heap and stack for storing the values of variables in memory (maybe link to something like [this](http://net-informations.com/faq/net/stack-heap.htm), for reference). – Casey Kuball Oct 05 '17 at 16:11
  • @Darthfett Yes, your right. I'll edit to clarify this. – Christian Dean Oct 05 '17 at 16:25