我想模擬python(2)中的對象的集合。該集合應該通過列表接口爲對象提供某個屬性(一個整數,浮點數或任何不可變的對象)。如何在python中實現引用列表?
(1)
>>> print (collection.attrs)
[1, 5, 3]
>>> collection.attrs = [4, 2, 3]
>>> print (object0.attr == 4)
True
我特別期望集合中的這個列表界面,以允許重新分配一個單獨的對象的屬性,例如
(2)
>>> collection.attrs[2] = 8
>>> print (object2.attr == 8)
True
我相信這是一個相當頻繁發生的情況下,遺憾的是我沒能找到如何實現它的計算器一個令人滿意的答案/谷歌等
在幕後,我預計object.attr
將作爲一個可變對象來實現。不知何故,我也希望收藏品擁有object.attr
的「參考列表」,而不是各自引用的(不可變的)值本身。
我問你的建議如何以優雅和靈活的方式解決這個問題。
的一種可能實現,它允許(1),但不用於(2)是
class Component(object):
"""One of many components."""
def __init__(self, attr):
self.attr = attr
class System(object):
"""One System object contains and manages many Component instances.
System is the main interface to adjusting the components.
"""
def __init__(self, attr_list):
self._components = []
for attr in attr_list:
new = Component(attr)
self._components.append(new)
@property
def attrs(self):
# !!! this breaks (2):
return [component.attr for component in self._components]
@attrs.setter
def attrs(self, new_attrs):
for component, new_attr in zip(self._components, new_attrs):
component.attr = new_attr
的!!!換行符(2),因爲我們創建了一個新列表,其條目是對所有Component.attr
的值的引用,而不是對屬性本身的引用。
感謝您的輸入。
TheXMA
要做到這一點,你可以使'System._components'成爲一個自定義類,它實現了你想要的__setitem__行爲。 – jonrsharpe 2015-02-10 08:51:50
完美,就像@filmor在下面做的一樣。 – TheXMA 2015-02-10 09:53:04