Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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]=5. Right now, this statement calls the getter function to get the whole list, gets the third item from the list, and sets that to 5. the myProp list is changed, but the second list never gets updated.

class Grid(object):

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

        # Make self._rows a multi dimensional array
        # with it's size width * height
        self._rows=[[None] * height for i in xrange(width)]
        # Make `self._columns` a multi dimensional array
        # with it's 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 columns 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

Example run:

>>> 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, this is where the actual problem occurs. I set the first item of the sublist to three, but foo.columns never updates itself to put the 3 in its list.

So in short, how do I make a variable that always updates another variable, even when it's subitem is being changed?

I'm using Python 2.7

like image 650
QxQ Avatar asked May 19 '12 14:05

QxQ


People also ask

What is property and setter in Python?

@property is used to get the value of a private attribute without using any getter methods. We have to put a line @property in front of the method where we return the private variable. To set the value of the private variable, we use @method_name. setter in front of the method.

What is the most pythonic way to use getter and setter?

Getters and Setters in python are often used when: We use getters & setters to add validation logic around getting and setting a value. To avoid direct access of a class field i.e. private variables cannot be accessed directly or modified by external user.

What does property () do in Python?

Python's property() is the Pythonic way to avoid formal getter and setter methods in your code. This function allows you to turn class attributes into properties or managed attributes. Since property() is a built-in function, you can use it without importing anything.

How does getter and setter help implement encapsulation in Python?

For the purpose of data encapsulation, most object oriented languages use getters and setters method. This is because we want to hide the attributes of a object class from other classes so that no accidental modification of the data happens by methods in other classes.


2 Answers

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.

like image 41
mata Avatar answered Oct 06 '22 00:10

mata


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
like image 52
Eric Avatar answered Oct 05 '22 23:10

Eric