Calling Class' Child Methods

S

Steve Holden

Andrea said:
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
 
K

Kent Johnson

Steve said:
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.

Another alternative is to delegate specific method by creating new attributes in MainClass. In MainClass.__init__() you can write
self.SomeMethod = self.customwidget.SomeMethod
to automatically delegate SomeMethod. You can do this from a list of method names:
for method in [ 'SomeMethod', 'SomeOtherMethod' ]:
setattr(self, method, getattr(self.customwidget, method))

This gives you more control over which methods are delegated - if there are some Custom methods that you do *not* want to expose in MainClass this might be a better approach.

Kent
 
A

Andrea Gavana

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)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top