[Python] Order of frames being pushed on the stack

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página