简体   繁体   English

单个列表元素的属性设置器

[英]Property setter for a single list element

All, I have a class with a several list objects, defined as follows: 所有,我都有一个带有几个列表对象的类,定义如下:

class Device:

  def __init__(self):
    self._channels = [None]*6
    self._outputs = [None]*4

  @property
  def channels(self):
    return self._channels

  @channels.setter
  def channels(self,value):
    print("inside:",self.channels, value)
    self._channels = value

The strange thing here is that calling device.channels[1] = 'try' works, but doesn't seem to 'go through' the @setter.channels function. 奇怪的是,调用device.channels[1] = 'try'device.channels[1] = 'try' ,但似乎并没有“通过” @ setter.channels函数。 The output from the following reveals the oddity: 以下内容显示了奇怪之处:

device = Device()
print("before:",device.channels)
device.channels[1] = "try"
print("after:",frmdeviced.channels)
device.channels = "try2"
print("check:",frm4d.channels)

and the output is: 输出为:


 before: [None, None, None, None, None, None] 
after: [None, 'try', None, None, None, None] # accessing single element is achieved
# , but not through @channels.setter!
inside: [None, 'try', None, None, None, None] try # only here we're
check: try2 # at least the setter works..

Since I require logic to run when a single element of channels is set, this behavior is problematic. 由于设置channels的单个元素时我需要运行逻辑,因此这种行为是有问题的。 I'm wondering what's the underlying python mechanism that causes this behavior, and how is it over-ridden? 我想知道导致此行为的底层python机制是什么,以及它如何被覆盖? Is there a more pythonic way to achieve the goal of setting/getting a specific list element? 有没有更Python的方法可以实现设置/获取特定列表元素的目标?

device.channels[1] = "try" is going to access "@property" getter method first, which return a list and then indexing operation would be performed on list not on device. device.channels[1] = "try"将首先访问"@property" getter方法,该方法将返回一个列表,然后对不在设备上的列表执行索引操作。 Below example demonstrate it- 下面的示例演示它-

>>> class Device:

  def __init__(self):
    self._channels = [None]*6
    self._outputs = [None]*4

  @property
  def channels(self):
    print("inside @property")
    return self._channels

  @channels.setter
  def channels(self,value):
    print("inside:",self.channels, value)
    self._channels = value


>>> device = Device()
>>> device.channels[1] = "try"
inside @property

To instrument the lists, create an instrumented list class. 要检测列表,请创建一个检测列表类。 Sample Jupyter session: 示例Jupyter会话:

In [23]: class MonitoredList(list):
             def __setitem__(self, index, value):
                 # run special logic here
                 print("setting index {} to {}".format(index, value))
                 super(MonitoredList, self).__setitem__(index, value)

In [24]: zz = MonitoredList([None]*6)

In [25]: zz
Out[25]: [None, None, None, None, None, None]

In [26]: zz[3] = 42
         setting index 3 to 42

In [27]: zz
Out[27]: [None, None, None, 42, None, None]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM