[Tutor] list of references to object properties
Jose Amoreira
ljmamoreira at gmail.com
Fri Jan 18 12:11:28 CET 2013
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?
Thanks,
Ze
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130118/528bb3e4/attachment.html>
More information about the Tutor
mailing list