__parent__ - like attribute?

Bengt Richter bokr at oz.net
Sun Jan 19 12:41:19 CET 2003


On Sat, 18 Jan 2003 21:52:00 -0800, Dylan Reinhardt <python at dylanreinhardt.com> wrote:

>At 09:00 PM 1/18/2003, Dennis Lee Bieber wrote:
>>        My understanding was that he wants changes /in/ the eggs instance to
>>affect the "changed" attribute of the spam instance.
>
>Yes, exactly.
>
>>         To which my response is: What is
>>
>>c = eggs()
>>c.do_stuff()
>>
>>supposed to be changing? The global namespace?
>
>I don't think so... but if that gets me where I need to go, I'm all ears.  :-)
>
>>class eggs(spam):
>>         def __init__(self):
>>                 spam.__init__(self)
>>
>>         def do_stuff(self):
>>                 self.changed = 1
>>                 ...
>
>I doubt inheritance will do the trick here.  Spam isn't a base class, it 
>defines a container object.
>
>To recap/clarify:
>
Why not write minimal code to illustrate?
>I have several instances of spam, each of which contains (let's say) many 
several_instances_of_spam = [Spam() for i in range(several)]

>thousands of instances of eggs.  I want for changes made to a single 
for spam_instance in serveral_instances_of_spam:
    for i in xrange(thousands):
        spam_instance.put_inside_spam(Eggs())

>instance of eggs to be made known to the specific instance of spam that 
>contains it.  Similarly, I want for any instance of eggs to be able to get 
>the value of any attribute of its containing spam object.
several_instances_of_spam[an_index_less_than_several].get_contained_egg(
    index_less_than_thousands).changed_attr = some_change

>
>To put this more generally, I want a subobject to find its containing 
What is your concept of "subobject"? If you set a = [b] is b a subobject
of a? I.e., simple binding of a reference in any manner into some part
of an object makes the referred-to object a subobject?


>object so that methods of the containing object can be called and 
>attributes of the containing object can be set/checked. I gather there's no 
see [1] below

>easy, pre-defined way to do this, so any namespace voodoo that can do the 
>trick would be most welcome.
>
Sure, there's any number of ways to do it. But to choose one, we need to know
how well/badly the above code reflects your problem world.

How are all the objects created? And how is an Egg instance made a subobject of
a Spam instance? I hypothesized that Spam objects would have a method to stuff
Egg instances into them serially, so the inside collection is implicitly known
by number. I.e, the statement from above that creates and stuffs new eggs into
spam instances is

    spam_instance.put_inside_spam(Eggs())

You notice I didn't even pass anything to the Egg constructor, but that would
be a place you could tell an egg what spam instance it was being put into, not
necessarily a direct reference, but you could, though it would create reference
cycles that you should manage carefully. E.g., if the Egg class were

    class Egg:
        def __init__(self, my_container):
            self.my_container = my_container

then

    spam_instance.put_inside_spam(Eggs(spam_instance))


would let any egg method reach the enclosing spam container via

    self.my_container

[1] and thus

    self.my_container.method_of_containing_object()
and
    self.my_container.attribute_of_containing_object = value_to_set_check

would do what you wanted. If self.my_container was not a reference, but an index,
you might use it like

    several_instances_of_spam = [Spam() for i in range(several)]
    for spam_index in xrange(len(several_instances_of_spam):
        spam_instance = several_instances_of_spam[spam_index]
        for i in xrange(thousands):
            spam_instance.put_inside_spam(Eggs(spam_index))

and then assuming several_instances_of_spam is global,

    several_instances_of_spam[self.my_container].method_of_containing_object()
and
    several_instances_of_spam[self.my_container].attribute_of_containing_object = value_to_set_check

If you are in control over everything, there are going to be better ways, but it's
not clear whether you are trying to tweak some big stuff you don't want to bother fully
understanding, or whether you have creative control. Either way has its difficulties, but
the latter is usually more fun, unless you run up a blind alley with a deadline and no ideas ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list