Search code examples
pythoncompiler-constructionstackvm-implementation

Order of frames being pushed on the stack


Suppose you have the following code.

def square(x):
    print ("Just before square returns")
    for k in dir():
        print ("{0} -------> {1}".format(k, eval(k)))
    return x*x
def cube(x):
    print ("Just before cube returns")
    for k in dir():
        print ("{0} -------> {1}".format(k, eval(k)))
    return x*x*x

x = 5
print ("cube(square({0})) = {1}".format(x, cube(square(x))))
print ("Just before main returns")
for k in dir():
    print ("{0} -------> {1}".format(k, eval(k)))

Running this code reveals the folllowing

Just before square returns
x -------> 5
Just before cube returns
x -------> 25
cube(square(5)) = 15625
Just before main returns
__builtins__ -------> <module 'builtins' (built-in)>
__cached__ -------> None
__doc__ -------> None
__file__ -------> exampleOne.py
__name__ -------> __main__
__package__ -------> None
cube -------> <function cube at 0x1037b78>
square -------> <function square at 0x1037af0>
x -------> 5

We are puzzled. Does the square function get pushed onto the stack first and have its return value computed and passed to the cube function? Another possibility is that the cube function is called first then, in the process of resolving the argument, the square function must be called. What can you tell us? Does this depend on compiler or language?


Solution

  • Well, of course it can depend on the language, but your example is Python.

    In Python, function arguments are always evaluated before the calling the function of which they are arguments. (See the documentation.) Since square(x) is passed as an argument of cube, square is called first, and then the result is passed to cube. In other words, function calls in an expression are always evaluated "from the inside out", with the innermost ones evaluated first. This is just like the order of evaluation of mathematical expressions (innermost parentheses first).