Skip to main content
Engineering LibreTexts

14.4: Function and Class Objects

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

    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.

    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 22.4.1 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.

    • Was this article helpful?