Recursive calls and stack

jm.suresh@no.spam.gmail.com jm.suresh at gmail.com
Wed Feb 14 02:23:46 EST 2007


On Feb 14, 11:45 am, "Gabriel Genellina" <gagsl... at yahoo.com.ar>
wrote:
> En Wed, 14 Feb 2007 03:09:37 -0300, jm.sur... at no.spam.gmail.com
> <jm.sur... at gmail.com> escribió:
>
> > Hi,
> >  I have a program which literately finds the object that overlapping a
> > point. The horizontal and vertical search are called recursively from
> > inside each other.
> >  Is this way of implementation fill the stack space with the local
> > variables inside each call. If this is not good, is there a better way
> > to implement? Or python itself will understand that the calls happen
> > in the last line, so local variables need not be pushed into the
> > stack?
>
> I'm afraid not, the calls will be stacked until some object is found.
> Python does not do "tail recursion optimization" (at least, I'm not aware
> of that). But even if it could do that, in this case you have recursive
> calls between two functions, and that's a bit harder.
>
> Going back to your original problem, maybe you can use some known
> algorithms from computational geometry; start with  http://www.faqs.org/faqs/graphics/algorithms-faq/
>
> --
> Gabriel Genellina

Thanks Gabriel for the response.

I am OK with calls being stacked, but I wondering will the local
variables be stacked given that return statement is followed by the
function call?

def test():
  x = 22
  y = 33
  z = x+y
  return anotherFunction(z)

In this function will all the local variables (x,y,z) be pushed into
the stack before calling anotherFunction(z) or Python will find out
that the locals are no longer needed as anotherFunction(z) is just
returned?

-
Suresh




More information about the Python-list mailing list