How to assign values to a slice of some @property
of a class, which happens to be a list
or numpy.ndarray
, so that it's setter is called? Now it seems that assignment to a slice calls only getter. I cannot understand how a slice is handled under the hood in such case. Example code:
class MyClass:
def __init__(self, a):
self.a = a
self._a = self.a
@property
def a(self):
return self._a
@a.setter
def a(self, v):
print("I'm called")
self._a = v
A = MyClass([1, 2, 3])
# prints "I'm called"
A.a = [4, 5, 6]
# prints "I'm called"
A.a[1] = 0
# does not print anything
How to make A.a[1] = 0
print "I'm called"
?