Page 239 -
P. 239
216 Appendix C. Lumpy
object Rectangle
corner
height
width
Point
x
y
Figure C.7: Class diagram.
But if you are passing functions and classes as parameters, you might want them to appear.
This example shows what that looks like; you can download it from http://thinkpython.
com/code/lumpy_demo6.py .
import copy
from swampy.Lumpy import Lumpy
lumpy = Lumpy()
lumpy.make_reference()
class Point(object):
"""Represents a point in 2-D space."""
class Rectangle(object):
"""Represents a rectangle."""
def instantiate(constructor):
"""Instantiates a new object."""
obj = constructor()
lumpy.object_diagram()
return obj
point = instantiate(Point)
Figure C.6 shows the result. Since we invoke object_diagram inside a function, we get
a stack diagram with a frame for the module-level variables and for the invocation of
instantiate .
At the module level, Point and Rectangle refer to class objects (which have type type );
instantiate refers to a function object.
This diagram might clarify two points of common confusion: (1) the difference between the
class object, Point , and the instance of Point, obj, and (2) the difference between the func-
tion object created when instantiate is defined, and the frame created with it is called.
C.5 Class Diagrams
Although I distinguish between state diagrams, stack diagrams and object diagrams, they
are mostly the same thing: they show the state of a running program at a point in time.