How to access a containing objects properties from an object inside.
nuwandame at hotmail.com
Thu Mar 5 17:33:22 CET 2009
Aaron Brady wrote:
> On Mar 5, 9:51 am, nuwandame <nuwand... at hotmail.com> wrote:
>> Aaron Brady wrote:
>>> On Mar 5, 8:44 am, nuwandame <nuwand... at hotmail.com> wrote:
>>>> I have two objects obj1 and obj2. Inside obj1 there is an attribute for
>>>> success (obj1.success) and for containing other objects (obj1.data)
>>>> I am using setattr() to add obj2 as an attribute to obj1.data
>>>> (obj1.data.obj2) this is working fine.
>>>> My problem is when someone changes a variable in obj2 instance after it
>>>> is added to obj1 e.g.
>>>> obj1.data.obj2.success = False
>>>> I am trying to figure out how to locate and access obj1.success when
>>>> obj2.success has changed.
>>>> Any assistance, pointers, ideas are much appreciated.
>>> Hi. There's no way in general, but if you will make a few
>>> assumptions, there are some possibilities. For example, you could
>>> make 'obj1.success' a descriptor, which searches its '__dict__', and
>>> looks for 'success' attributes in its contents.
>>> How does that strike you?
>> You suggest an interesting idea which triggered another idea...
>> Are there mechanisms for using, accessing, executing the object id?
>> objid = id(obj1)
>> If so, I could set that as an attribute in the subsequent object when
>> adding it as an attribute and then call it when values of that attribute
> I think what you are after is a weak value dictionary. But why not
> just store the parent as an attribute? obj2.parent= obj1.
Thanks a bunch.
More information about the Python-list