当属性是对象列表时,如何识别属性更改

时间:2019-05-16 09:59:06

标签: python

矩形对象如何监听点属性x和y,如果它们发生变化,矩形对象将重新计算面积吗?

如果我使用setter和getter进行此操作,则每次访问area属性时,都会重新计算面积。如果计算非常昂贵(我在这里还要做更多的事情),那么这对我来说不是最佳解决方案。是否可以听取积分,仅在积分改变时重新计算面积?

我有一个叫Rectangle的类和一个叫Point的类:

class Point(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y


class Rectangle(object):

    def __init__(self, points=None):
        self.points = [] if points is None else points
        self.area = self.calc_area()

    def calc_area(self):
        return (self.points[0].x - self.points[1].x) * (self.points[0].y - self.points[1].y)

然后我创建两个点和一个包含两个点的矩形:

# create the points:
points = list()
points.append(Point(0,0))
points.append(Point(1,1))

# create the rectangle:
rect = Rectangle(points)
print(rect.area)

现在,我更改第一个点的坐标:

# change the points coordinates:
points[0].x = 0.5
points[0].y = 0.5

# Now the area should be recalculated.
print(rect.area)

3 个答案:

答案 0 :(得分:2)

解决方案:

您可以将area声明为property

class Rectangle(object):
    def __init__(self, points=list()):
        self.points = points
        # self.area = self.calc_area() -- removed

    @property
    def area(self):
        return = (self.points[0].x - self.points[1].x) * (self.points[0].y - self.points[1].y)

它将解决问题。

更新。

如果您希望仅在值更改时才重新计算面积,则可以使用自定义标志并将其设置在属性设置器上。

代码:

class Point(object):
    def __init__(self, x, y):
        self._x = x
        self._y = y
        self.updated = True

    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, value):
        self.updated = True
        self._x = value

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self, value):
        self.updated = True
        self._y = value


class Rectangle(object):
    def __init__(self, points=None):
        self.points = [] if points is None else points
        self._area = 0

    @property
    def area(self):
        if any(point.updated for point in self.points):
            self._area = (self.points[0].x - self.points[1].x) * (self.points[0].y - self.points[1].y)
            for point in self.points:
                point.updated = False
            print("recalculated") # delete it, it's just for test
        return self._area


points = [Point(0, 0), Point(1, 1)]

rect = Rectangle(points)
print(rect.area)
print(rect.area)

points[0].x = 0.5
points[0].y = 0.5

print(rect.area)

输出:

recalculated
1
1
recalculated
0.25

答案 1 :(得分:1)

您可以:

  • 将变量x和y设为私有,并使用getter和setter进行访问,并且在setter中还更新区域
  • 不需要访问rect.area,只需在需要时致电rect.calc_area()

答案 2 :(得分:0)

谢谢@OlvinR​​oght,我认为您链接中的问题是实现此目的的最佳解决方案。所以现在我实现了观察者模式。在这里,我可以将“矩形点”列表中的每个点绑定到更新函数。

class Point(object):

    def __init__(self, x, y):
        self._x = x
        self._y = y
        self._observers = []

    @property
    def x(self):
        return self._x

    @property
    def y(self):
        return self._y

    @x.setter
    def x(self, value):
        self._x = value
        for callback in self._observers:
            print('announcing change')
            callback()

    @y.setter
    def y(self, value):
        self._y = value
        for callback in self._observers:
            print('announcing change')
            callback()

    def bind_to(self, callback):
        print('bound')
        self._observers.append(callback)


class Rectangle(object):

    def __init__(self, points=None):
        self.points = [] if points is None else points
        self.area = []

        for point in self.points:
            point.bind_to(self.update_area)
        self.area = (self.points[0].x - self.points[1].x) * (self.points[0].y - self.points[1].y)

    def update_area(self):
        print('updating area')
        self.area = (self.points[0].x - self.points[1].x) * (self.points[0].y - self.points[1].y)


if __name__ == '__main__':

    # create points:
    points = list()
    points.append(Point(0, 0))
    points.append(Point(1, 1))

    # create the rectangle:
    rect = Rectangle(points)
    print('Area = {}'.format(rect.area))

    # change point coordinates
    points[0].x = 0.5
    points[0].y = 0.5
    print('Area = {}'.format(rect.area))

    # change point coordinates again:
    points[0].x = 0.25
    points[0].y = 0.25
    print('Area = {}'.format(rect.area))

    # just print the area; the area is not recalculated:
    print('Area = {}'.format(rect.area))

输出:

Area = 1
announcing change
updating area
announcing change
updating area
Area = 0.25
announcing change
updating area
announcing change
updating area
Area = 0.5625
Area = 0.5625