471,853 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,853 software developers and data experts.

Calling Class' Child Methods

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.
--
"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77
Nov 5 '05 #1
2 4101
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/Coo...n/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/

Nov 5 '05 #2
Steve Holden wrote:
Andrea Gavana wrote:
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.


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
Nov 5 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Axel Straschil | last post: by
5 posts views Thread by Da Costa Gomez | last post: by
9 posts views Thread by Martin Herbert Dietze | last post: by
5 posts views Thread by Dave Veeneman | last post: by
3 posts views Thread by Horace | last post: by
1 post views Thread by =?Utf-8?B?cmFuZHkxMjAw?= | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.