473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 4200
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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.