[Tutor] list of references to object properties

Peter Otten __peter__ at web.de
Fri Jan 18 12:37:26 CET 2013


Jose Amoreira wrote:

> Hello
> Suppose I have a list l_obj of similar objects. Is there any way I can
> generate a list l_prp of references to a given property of those objects
> in such a way that, if change the value of one element in l_prp, the
> corresponding object in l_obj gets its property updated, and vice-versa?
> Let give an example of what I have in mind.
> 
> In [1]: class MyCls(object):
> 
>    ...: def __init__(self,a):
> 
>    ...: self.prp = a
> 
>    ...:
> 
> In [2]: l_obj = [MyCls(float(i)) for i in range(3)]
> 
> In [3]: l_prp = [item.prp for item in l_obj]
> 
> In [4]: for ob in l_obj:
> 
>    ...: print ob.prp,
> 
>    ...:
> 
> 0.0 1.0 2.0
> 
> In [5]: l_prp
> 
> Out[5]: [0.0, 1.0, 2.0]
> 
> In [6]: l_prp[1]=5.
> 
> In [7]: l_obj[1].prp
> 
> Out[7]: 1.0
> 
> As expected, changes in l_prp do not change the properties of the elements
> in l_obj, neither do changes in l_obj's element's properties change the
> values in l_prp.
> 
> Is there a simple way to implement such connections?

No. You'd need something like the observer pattern (listeners in Java), 
where the class owning the property (MyCls) has to cooperate. The 
administrative overhead is relatively high.

The pythonic way is to regenerate the l_prp list every time you need an up-
to-date overview of the current values.

An intermediate approach is to turn l_prp into a view on the l_obj list:

from collections import Sequence

class A(object):
    def __init__(self, attrib):
        self.attrib = attrib

class AttribView(Sequence):
    def __init__(self, items):
        self._items = items
    def __getitem__(self, index):
        return self._items[index].attrib
    def __len__(self):
        return len(self._items)
    def __repr__(self):
        return repr(list(self))

items = [A(c) for c in "abcde"]
attribs = AttribView(items)
print attribs
items[1].attrib = 42
print attribs




More information about the Tutor mailing list