python: how to have a property and with a setter function that detects all changes that happen to the value

前端 未结 3 2013
栀梦
栀梦 2020-12-18 07:50

I have a two properties which holds lists. Whenever any item in this list changes, I would like the other list to update itself. This includes the statement obj.myProp

相关标签:
3条回答
  • 2020-12-18 08:24

    I recommend you look into using numpy. In particular, the transpose method gives a "view" of the underlying matrix with the axes transposed, meaning that if you change one then the other will change.

    If that doesn't suit your needs, then the only way I can see to make the API you want work is to define the "rows" and "columns" methods to return custom "view objects" that don't actually store the data, but that point back to some private (shared) data in the grid object itself. These could be list-like, but would not support some operations (e.g., append or clear).

    0 讨论(0)
  • 2020-12-18 08:28

    if you simply return your rows or columns list, then you'll never have any control about what happens when an item is changed.

    one possibility would be not to provide properties to get/set the lists directly, but provide setters/getters for an item at position x/y.

    a nice version would be to have __setitem__/__getitem__ and have them accept a tuple, that way you could acces the elements using foo[x,y] and foo[x,y] = bar.

    an other way would be to return a wrapper around the list that detects whe an element is changed, but then you'll also have to do that for every nested list.

    0 讨论(0)
  • 2020-12-18 08:30

    Your problem is that you're not setting foo.rows on the offending line - you're getting it, and then modifying one of it's members. This isn't going to fire the setter. With the API you're proposing, you would need to return a list that has getters and setters attached as well.

    You'd do better to not use the rows and columns properties to set entries, and add a __getitem__ method like this:

    class Grid(object):
    
        def __init__(self, width=0, height=0):
            self._data = [None] * width * height;
            self.width = width
            self.height = height
    
        def __getitem__(self, pos):
            if type(pos) != tuple or len(pos) != 2:
                raise IndexError('Index must be a tuple of length 2')
            x, y = pos
            if 0 <= x < self.width and 0 <= y < self.height:
                return self._data[x + self.width * y]
            else:
                raise IndexError('Grid index out of range')
    
        def __setitem__(self, pos, value):
            if type(pos) != tuple or len(pos) != 2:
                raise IndexError('Index must be a tuple of length 2')
            x, y = pos
            if 0 <= x < self.width and 0 <= y < self.height:
                self._data[x + self.width * y] = value
            else:
                raise IndexError('Grid index out of range')
    
        @property
        def columns(self):
            return [
                [self[x, y] for x in xrange(self.width)]
                for y in xrange(self.height)
            ]
    
        @property
        def rows(self):
            return [
                [self[x, y] for y in xrange(self.height)]
                for x in xrange(self.width)
            ]
    

    The broken line then becomes:

    foo[0, 0] = 3
    
    0 讨论(0)
提交回复
热议问题