Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python property on a list

I am new to Python and I just discovered the properties. It works just fine when I try it on a simple variable but I cannot make it work on a list. When executing the code below it will call two times the getter and not the setter. I know that in my example the property has no added value but it is to simplify.

class C:
    def __init__(self):
        self._x = [1, 2, 3]

    @property
    def x(self):
        print("getter")
        return self._x
    @x.setter
    def x(self, value):
        print("setter")
        self._x = value

c = C()
c.x[1] = 4
print(c.x[1])

Does anybody have an idea about what I'm doing wrong ?

like image 396
DoubleT28 Avatar asked Jun 01 '16 09:06

DoubleT28


1 Answers

The setter/getter are only used if you directly get or set the property:

c.x           # getter
c.x = [1,2,3] # setter

If you modify an element in the property you get the property and then set the corresponding element. Your example is equivalent to

d = c.x       # getter again
d[1] = 4

You could also use __getitem__ and __setitem__ to directly allow setting and getting specific items.

class C:
    def __init__(self):
        self._x = [1, 2, 3]

    @property
    def x(self):
        print("getter")
        return self._x
    @x.setter
    def x(self, value):
        print("setter")
        self._x = value

    def __getitem__(self, idx):
        print("getitem")
        return self._x[idx]

    def __setitem__(self, idx, value):
        print("setitem")
        self._x[idx] = value

>>> c = C()
>>> c[1] = 3
setitem
>>> c.x
getter
[1, 3, 3]
>>> c[2]
getitem
3
like image 122
MSeifert Avatar answered Sep 22 '22 07:09

MSeifert