Skip to main content
Engineering LibreTexts

14.2: Stack Diagram

  • Page ID
  • Stack diagram.
    Figure \(\PageIndex{1}\): Stack diagram.

    Here’s an example that uses Lumpy to generate a stack diagram. You can download it from

    from swampy.Lumpy import Lumpy
    def countdown(n):
        if n <= 0:
            print 'Blastoff!'
            print n
    lumpy = Lumpy()

    Figure 22.2.1 shows the result. Each frame is represented with a box that has the function’s name outside and variables inside. Since this function is recursive, there is one frame for each level of recursion.

    Remember that a stack diagram shows the state of the program at a particular point in its execution. To get the diagram you want, sometimes you have to think about where to invoke object_diagram.

    In this case I invoke object_diagram after executing the base case of the recursion; that way the stack diagram shows each level of the recursion. You can call object_diagram more than once to get a series of snapshots of the program’s execution.