[Tutor] references to containing objects
Kent Johnson
kent37 at tds.net
Mon Jun 23 12:54:11 CEST 2008
On Mon, Jun 23, 2008 at 12:17 AM, John Gunderman
<meanburrito920 at yahoo.com> wrote:
> I am looking for a way to tell a object the properties of its containing
> object.
> For example, I have an object foo, of class Bar, which I have stored in a
> dict
> in the object I want to access. Basically:
>
> container_object.contained_object["foo"].action()
>
> What I want is for the object "foo" to be able to access properties of
> container_object
> through its action() method.
Maybe the actions can be bound methods of the container object class?
For example:
class Container(object):
def __init__(self):
self.actions = {}
self.actions['foo'] = self.foo
self.value = 3
def foo(self):
print 'foo', self.value
c = Container()
c.actions['foo']()
Otherwise I would go with Alan's suggestion of passing the parent to
the action class constructor.
Kent
More information about the Tutor
mailing list