Page 237 - thinkpython
P. 237
C.4. Function and class objects 215
<module> Rectangle
box width 100.0
height 200.0
Point
corner y 0.0
Rectangle
x 0.0
box2 corner
height 200.0
width 100.0
Figure C.5: Object diagram.
<module> type instantiate
Point
Point __name__ 'Point' obj
function
constructor
instantiate __name__ 'instantiate'
type
Rectangle __name__ 'Rectangle'
Figure C.6: Object diagram.
lumpy = Lumpy()
lumpy.make_reference()
box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 0.0
box.corner.y = 0.0
box2 = copy.copy(box)
lumpy.object_diagram()
Figure C.5 shows the result. copy.copy make a shallow copy, so box and box2 have their
own width and height , but they share the same embedded Point object. This kind of
sharing is usually fine with immutable objects, but with mutable types, it is highly error-
prone.
C.4 Function and class objects
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.