7
votes

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 ?

2

2 Answers

8
votes

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
0
votes

Your c.x[1] = 4 is in fact split into two operations:

  1. get the "x" property of c (=> getter)
  2. set item on the resulting object.

There is no easy way how to get out of this. But anyway, in the end it does what you wanted, no?

The setter would be called, if you wrote something like:

c.x = another_list