Accessing container's methods

Michael Torrie torriem at gmail.com
Mon Dec 7 13:36:02 EST 2015


On 12/07/2015 11:10 AM, Tony van der Hoff wrote:
> Hi,
> 
> I have a class A, containing embedded embedded classes, which need to 
> access methods from A.
> .
> A highly contrived example, where I'm setting up an outer class in a 
> Has-a relationship, containing a number of Actors. The inner class needs 
> to access a method of the outer class; here the method get_name.
> 
> I don't really want to make Actor a sub-class (is-a; it isn't) of Monty; 
> that would raise all sorts of other problems.
> 
> Can anyone please advise me on how to achieve this magic?

You could add an attribute to each embedded object that provides a
reference back to the container object.

All in all, this design has a kind of smell to it.  Would it not be
better to ask the container class for information about the children,
rather than the other way around?  If a piece of code holds a reference
to the child object then it must also by definition hold a reference to
the container object, so why can't it ask the container object directly?



More information about the Python-list mailing list