You are here

C.4 Function and class objects

20 January, 2016 - 15:50

When I use Lumpy to make object diagrams, I usually define the functions and classes before I make the reference point. That way, function and class objects don’t appear in the diagram.

Figure 19.9 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

import copy
from swampy.Lumpy import Lumpy
lumpy = Lumpy()
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()
return obj
point = instantiate(Point)

Figure 19.8 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 function object created when instantiate is defined, and the frame created with it is called.