如何使切片列表@property在python中调用它的setter

时间:2018-05-05 09:55:44

标签: python properties slice getter-setter

如何将值分配给某个类@property的某个切片,该切片恰好是listnumpy.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"

0 个答案:

没有答案