6.4: Instances as Return Values
- Page ID
- 15449
Functions can return instances. For example, find_center
takes a Rectangle
as an argument and returns a Point
that contains the coordinates of the center of the Rectangle
:
def find_center(rect): p = Point() p.x = rect.corner.x + rect.width/2.0 p.y = rect.corner.y + rect.height/2.0 return p
Here is an example that passes box
as an argument and assigns the resulting Point to center
:
>>> center = find_center(box) >>> print_point(center) (50.0, 100.0)