__parent__ - like attribute?

Dylan Reinhardt python at dylanreinhardt.com
Sun Jan 19 11:15:45 EST 2003


At 03:41 AM 1/19/2003, Bengt Richter wrote:
>Why not write minimal code to illustrate?


My first post had this minimal code example:


-------------------------

class spam:
     __init__(self):
         self.stuff = eggs()
         self.changed = 0

class eggs:
     def do_stuff(self):
         # after we've done the main part of our work,
         # signal parent object that information has changed
         self.__some_namespace_voodoo__.changed = 1
         return something_unrelated

a = spam()
b = spam()
x = a.stuff.do_stuff()   #  <--- should set value of a.changed to 1
x = b.stuff.do_stuff()   #  <--- should set value of b.changed to 1

-------------------------


>several_instances_of_spam[an_index_less_than_several].get_contained_egg(
>     index_less_than_thousands).changed_attr = some_change


The whole point of doing it the way I'm doing it is to provide a clean, 
Pythonic interface, thus:

spam_instance.egg_instance.egg_method()

should have a side effect on the spam instance.

>What is your concept of "subobject"?

Hopefully this should be clear from the code example.

>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.

Does the provided code do it?

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


This is actually what I'm doing now.  If this is the best available answer, 
I can certainly live with that, but it been surprised at what some of the 
namespace wizards here have come up with in the past, so I thought I'd see 
if there is a more elegant/efficient answer.


>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.

The latter.  I'm an experienced developer working on code I have total 
control over.

Thanks in advance,

Dylan






More information about the Python-list mailing list