您在這裡

Instances as return values

8 九月, 2015 - 10:43

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.0p.y = rect.corner.y + rect.height/2.0return 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)