Generarl programming question.
tjreedy at udel.edu
Sat Apr 11 20:47:51 CEST 2015
On 4/11/2015 12:23 PM, Thomas 'PointedEars' Lahn wrote:
> Chris Angelico wrote:
>> 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.
> Thank you for that description; I shall use it from now on when teaching
> laymen about the call stack.
What Chris is describing is one local namespace (sheet of paper) per
function *call*. In early Fortran (at least the first version I used),
there was one local namespace (sheet) per *function*. The call stack
was a stack of (pointers to) functions. While a function object was in
use (after a call, before the return), it could not be called again. In
other words, recursion, direct or indirect, was not allowed. I believe
the same was (is?) true of some versions of BASIC.
It has been proposed that Python use a hybrid model. Function objects
would have space for local variables for the first call, but there would
also be a mechanism to allocate additional 'sheets' for recursive calls.
The idea is that most functions are not called recursively, so the
overhead of allocating and freeing the per-call space is usually not
needed. I do not believe that anyone has implemented the idea to test
feasibility and the actual speedup in relation to the additional complexity.
Terry Jan Reedy
More information about the Python-list