You are here

C.2 Stack diagram

20 January, 2016 - 15:50

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

Figure 19.5 Figure C.3: Object diagram. 
from swampy.Lumpy import Lumpy
def countdown(n):
if n <= 0:
print 'Blastoff!'
print n
lumpy = Lumpy()

Figure 19.4 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.