473,609 Members | 2,134 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.Pa nel):

def __init__(self, *args, **kwds):

wx.Panel.__init __(self, *args, **kwds)

self.childpanel = wx.Panel(self, -1)
self.customwidg et = Custom(self, -1)

layoutsizer = wx.BoxSizer(wx. VERTICAL)
layoutsizer.Add (self.childpane l, 1)
layoutsizer.Add (self.customwid get)
layoutsizer.Lay out()
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.customwidg et.SomeMethod(p aram)

But the "Custom" class has *a lot* of methods, so I will end up in rewriting
all the "SomeMethod s" in the MainClass just to pass the parameters/settings
to self.customwidg et. 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.customwidg et to a layoutsizer. How can I
write:

layoutsizer = wx.BoxSizer(wx. VERTICAL)
layoutsizer.Add (self.childpane l, 1)
layoutsizer.Add (self) # <=== That's impossible
layoutsizer.Lay out()

?

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

def __init__(self, *args, **kwds):

wx.Panel.__init __(self, *args, **kwds)

self.childpanel = wx.Panel(self, -1)
self.customwidg et = Custom(self, -1)

layoutsizer = wx.BoxSizer(wx. VERTICAL)
layoutsizer.Add (self.childpane l, 1)
layoutsizer.Add (self.customwid get)
layoutsizer.Lay out()
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.customwidg et.SomeMethod(p aram)
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.__get attr__() so it returns
the appropariate attribute from self.customwidg et.

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 "SomeMethod s" in the MainClass just to pass the parameters/settings
to self.customwidg et. 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.customwidg et to a layoutsizer. How can I
write:

layoutsizer = wx.BoxSizer(wx. VERTICAL)
layoutsizer.Add (self.childpane l, 1)
layoutsizer.Add (self) # <=== That's impossible
layoutsizer.Lay out()

?

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.customwidg et.SomeMethod(p aram)

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.__ini t__() you can write
self.SomeMethod = self.customwidg et.SomeMethod
to automatically delegate SomeMethod. You can do this from a list of method names:
for method in [ 'SomeMethod', 'SomeOtherMetho d' ]:
setattr(self, method, getattr(self.cu stomwidget, 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
1544
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 scripting level which is in a different namespace than the dynamically created class. I can call the method, but only by passing strings and using getattr.I find this inelegant. I would rather pass a callable object, but cannot figure out how to...
14
6390
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 is the right and common case to call init: class Mother(object): def __init__(self, param_mother): print 'Mother'
5
6522
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() { super.f(); ... } in the child to first execute the parent stuff to be followed by the
9
5117
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 half-code this should in the end look like this: In the child class:
5
20023
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 it somewhat confusing when I look at the code later, because I expect to find a method called MyMethod() in the derived class. I think C# would let me call a base class method from a derived class like
9
7549
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 be implemented. So if you have a class which inherits base class that inherts an interface, then your classes will have a standard. I suppose you can also check for interface at run time say when dll is loaded and see if it implememts whats...
0
1163
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 exposes several public methods. I coded a HTML page and placed (by code) my custom control. I also placed a button on the same page. Every time I click on the button, it calls one of
3
1129
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 Imports Tiger_Devl.Includes.Child2 Namespace Includes
1
6523
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 Talker() in Parent will call the Talker() method in every child class. The kicker is that I have many different Child classes, and not all Child classes will be loaded when Talker() in the Parent is called. Thanks, Randy
0
8127
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8067
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8567
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8527
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8215
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5509
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2529
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 we have to send another system
0
1380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.