En Wed, 14 Feb 2007 04:23:46 -0300, [EMAIL PROTECTED] <[EMAIL PROTECTED]> escribió:
> 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? Not exactly like "pushed into the stack", but there exists a frame object, and it contains references to its local variables, and will be alive until anotherFunction returns. From inside anotherFunction you can even inspect those variables: py> def test(): ... x = 22 ... y = 33 ... z = x+y ... return anotherFunction(z) ... py> def anotherFunction(z): ... print "previous frame:", sys._getframe(1).f_locals ... py> test() previous frame: {'y': 33, 'x': 22, 'z': 55} So x,y,z will still be there until anotherFunction actually returns to test, and the frame object is disposed. For a highly recursive function, that's bad news. For debugging normal programs, that's good news; the cgitb module, by example, is able to show the values for local variables in all previous frames when an exception is raised. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list