Python: how to have a property with a setter function that detects all changes that happen to a value

I have two properties that contain lists. whenever any item in this list changes, I would like another list to be updated. this includes the statement obj.myProp [3] = 5. Right now, this statement calls the getter function to get the whole list, gets the third element from the list and sets it to 5. The myProp list is changed, but the second list is never updated.

here is my code

class grid(object):

    def __init__(self,width=0,height=0):

        #make self._rows a multi demensional array
        #with it size width*height
        self._rows=[  [None]*height for i in xrange(width)  ]
        #make self._columns a multi demensional array
        #with it size height*width
        self._columns=[  [None]*width for i in xrange(height)  ]

    @property
    def rows(self):#getting the rows of the array
        return self._rows
    @rows.setter
    def rows(self,value):#when the rows are changed, the collumns are updated.
        self._rows=value
        self._columns=self._flip(value)

    @property
    def columns(self):#getting the columns of the array
        return self._columns
    @columns.setter
    def columns(self,value):#when the columns are changed, the rows are updated
        self._columns=value
        self._rows=self._flip(value)

    @staticmethod
    def _flip(args):#this flips the array
        ans=[  [None]*len(args) for i in xrange(len(args[0]))  ]
        for x in range(len(args)):
            for y in range(len(args[0])):
                ans[y][x] = args[x][y]
        return ans

and here is an example of execution.

>>> foo=grid(3,2)
>>> foo.rows
[[None, None], [None, None], [None, None]]
>>> foo.columns
[[None, None, None], [None, None, None]]
>>> foo.rows=[[1,2,3],[10,20,30]]
>>> foo.rows
[[1, 2, 3], [10, 20, 30]]
>>> foo.columns
[[1, 10], [2, 20], [3, 30]]
>>> foo.rows[0][0]=3
>>> foo.rows
[[3, 2, 3], [10, 20, 30]]
>>> foo.columns
[[1, 10], [2, 20], [3, 30]]

if you look at the last three lines, the real problem arises here. I set the first subscription element to three, but foo.column never updates itself to put list 3 on it.

, , , .

python 2.7

+5
3

, foo.rows - , . . API, , , .

__getiten__ :

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)
        ]

:

foo[0, 0] = 3
+5

rows columns, , , .

, / , / x/y.

__setitem__/__getitem__ , foo[x,y] foo[x,y] = bar.

, , , , .

+2

numpy. , transpose "" , , , .

, , , - "" ​​ "" " ", , () . , (, ).

0

All Articles