473,511 Members | 15,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4207
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1535
by: Danny Shevitz | last post by:
Howdy, I am trying to call class methods that have been created via a "type" function. I have enclosed a simplified example that shows what I am trying to do. In particular I am calling from the...
14
6378
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
5
6506
by: Da Costa Gomez | last post by:
Hi, I was wondering whether someone could shed some light on the following. Using inheritance in Java one can override a function f() (or is it overload?) in the child and then do: public f() {...
9
5106
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In...
5
20007
by: Dave Veeneman | last post by:
I'm using inheritance more than I used to, and I find myself calling a lot of base class methods. I generally call a base method from a dreived class like this: this.MyMethod(); I'm finding...
9
7540
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
0
1160
by: Carlitos | last post by:
Hi there, I apologize if it is not the right forum to post this question, but it has to do with C#, HTML and javascript altogether. I programmed a windows form custom control in C# which...
3
1126
by: Horace | last post by:
Hello I have a class Parent1 that instantiates Child1 and Child2. But Child1 calls methods in Child2 like so :- ======================================== Imports Tiger_Devl.Includes.Child1...
1
6508
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.