Calling Class' Child Methods

Steve Holden steve at holdenweb.com
Fri Nov 4 20:40:51 EST 2005


Andrea Gavana wrote:
> Hello NG,
> 
>     this may seem a stupid (or even impossible) question, but my knowlegde
> of Python is quite limited. I have basically a simple graphical user
> interface that contains a Panel, another panel (child of the main panel) and
> a custom widget (child of the main panel). Basically is something like (only
> some small code, is in wxPython but the question is more Python-related):
> 
> class MainClass(wx.Panel):
> 
>     def __init__(self, *args, **kwds):
> 
>         wx.Panel.__init__(self, *args, **kwds)
> 
>         self.childpanel = wx.Panel(self, -1)
>         self.customwidget = Custom(self, -1)
> 
>         layoutsizer = wx.BoxSizer(wx.VERTICAL)
>         layoutsizer.Add(self.childpanel, 1)
>         layoutsizer.Add(self.customwidget)
>         layoutsizer.Layout()
> 
> 
> The class "Custom" has a lot of methods (functions), but the user won't call
> directly this class, he/she will call the MainClass class to construct the
> GUI app. However, all the methods that the user can call refer to the
> "Custom" class, not the MainClass class. That is, the methods that the user
> call should propagate to the "Custom" class. However, I know I can do:
> 
> # Inside MainClass
>     def SomeMethod(self, param):
>         self.customwidget.SomeMethod(param)
> 
It seems that what you need is a generic delegation.

This pattern (in Python, anyway) makes use of the fact that if the 
interpreter can't find a method or other attribute for an object it will 
call the object's __getattr__() method.

So, what yo need to do is define MainClass.__getattr__() so it returns 
the appropariate attribute from self.customwidget.

You'll find in

     http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52295

a discussion and examples dating from before new-style classes ("types") 
were introduced into Python, but Alex Martelli's exposition is hard top 
beat.

> But the "Custom" class has *a lot* of methods, so I will end up in rewriting
> all the "SomeMethods" in the MainClass just to pass the parameters/settings
> to self.customwidget. Moreover, I know I can do (in the __init__ method of
> MainClass):
> 
>     def __init__(self, *args, **kwds):
> 
>         wx.Panel.__init__(self, *args, **kwds)
>         Custom.__init__(self, parent, -1)
> 
> In order to make MainClass knowing about the Custom methods. But the I will
> not be able (I suppose) to add self.customwidget to a layoutsizer. How can I
> write:
> 
> layoutsizer = wx.BoxSizer(wx.VERTICAL)
> layoutsizer.Add(self.childpanel, 1)
> layoutsizer.Add(self)   # <=== That's impossible
> layoutsizer.Layout()
> 
> ?
> 
> So (and I am very sorry for the long and maybe complex to understand post,
> english is not my mother tongue and I am still trying to figure out how to
> solve this problem), how can I let MainClass knowing about the Custom
> methods without rewriting all the Custom functions inside MainClass and then
> pass the parameters to Custom? Is there a way to "propagate" the methods to
> the child class (Custom)?
> 
> Thanks for every suggestion, and sorry for the long post.
> 
> Andrea.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list