如何将值分配给某个类@property
的某个切片,该切片恰好是list
或numpy.ndarray
,以便调用它的setter?现在看来,对切片的赋值只调用getter。在这种情况下,我无法理解如何在引擎盖下处理切片。示例代码:
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
如何A.a[1] = 0
打印"I'm called"
?