473,721 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bend or redirect a class method and then call it again

Hello,

(in wxPython)

I want to redirect the SetStatusText
to an own method (change the text somewhat) and
then call the SetStatusText of wx.Frame.

But I have a little problem of understanding, but I think,
it should be achieved somehow.

the output of "show_statu s" should be then:
"mytext: hallo"

class MyFrame (wx.Frame):
def show_status(sel f):
SetStatusText ("hallo")

my_frame = Myframe

Plugin (my_frame)

def Plugin(frame_in st)

#here i want to redirect the setstatustext

setattr(frame_i nst, 'SetStatusText' , NewStatusText)

def SetStatusText(t e,text,nr=0):
super(MyFrame, MyFrame).SetSta tusText("mytext : " + te)
#this is erronous

def NewStatusText(s , n, n1= 0):
SetStatusText(s , n, n1)

Is this possible?
How can I get this working?

thank you in advance
--
Franz Steinhaeusler
Jul 18 '05 #1
8 1517
Franz Steinhaeusler wrote:
I want to redirect the SetStatusText
to an own method (change the text somewhat) and
then call the SetStatusText of wx.Frame.
You are being a bit informal with what is a method and what is a
function. Your examples don't even parse. I think this informality
may be the source of your problem.
class MyFrame (wx.Frame):
def show_status(sel f):
SetStatusText ("hallo")
my_frame = Myframe perhaps you mean:
my_frame = Myframe() Plugin (my_frame) def Plugin(frame_in st) No colon above
#here i want to redirect the setstatustext
setattr(frame_i nst, 'SetStatusText' , NewStatusText) You seem to be trying to create a subclass dynamically.
def SetStatusText(t e,text,nr=0):
super(MyFrame, MyFrame).SetSta tusText("mytext : " + te)
#this is erronous The super call is being done inside a function, not a class.


Is this possible?
How can I get this working?


Does this address your problem?:

class MyFrame(wx.Fram e):
def show_status(sel f):
SetStatusText(" hallo")

def SetStatusText(s elf, te, text, nr=0):
super(MyFrame, self).SetStatus Text("mytext: " + te)

-Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #2
On Wed, 21 Jul 2004 12:21:33 -0700, Scott David Daniels
<Sc***********@ Acm.Org> wrote:
[...]
Does this address your problem?:

class MyFrame(wx.Fram e):
def show_status(sel f):
SetStatusText(" hallo")

def SetStatusText(s elf, te, text, nr=0):
super(MyFrame, self).SetStatus Text("mytext: " + te)


thank you,

I was not exact enough.

It was not so easy, because I had to apply the super in a function
and not in the class itself.

but finally i got it to work:

#drFrame is inherited from wx.Frame
#in drFrame there are some calls to SetStatusText

import wx
from drpython import DrFrame

def Plugin(DrFrame) :

def SetStatusText(t ext, col):
if col == 1:
#text.find
ind1 = text.find ("Col: ")
if ind1 > -1:
ind1 += len ("Col: ");
ind2 = text.find (" ", ind1)
nr = int (text [ind1:ind2]) + 1
text = text [:ind1] + str (nr) + text[ind2:]
super(type(DrFr ame), DrFrame).SetSta tusText(text, col)

def NewStatusText(t ext, col=0):
SetStatusText(t ext, col)

setattr(DrFrame , 'SetStatusText' , NewStatusText)
Is there a better/simpler/clearer solution?

thank you
--
Franz Steinhaeusler
Jul 18 '05 #3
Franz Steinhaeusler wrote:
#drFrame is inherited from wx.Frame
#in drFrame there are some calls to SetStatusText

import wx
from drpython import DrFrame

def Plugin(DrFrame) :

def SetStatusText(t ext, col):
#[...]

def NewStatusText(t ext, col=0):
SetStatusText(t ext, col)

setattr(DrFrame , 'SetStatusText' , NewStatusText)

What's the point of this extra 'NewStatusText' method? You're already
overriding SetStatusText. Without the NewStatusText and the setattr()
call, when someone calls SomePluginObjec t.SetStatusText (), it will call
your new version rather than the wx.Frame (or DrFrame) version -- that's
the whole point of inheritance. (This is true even when the 'someone'
is the object itself, from within the DrFrame base class. The *only*
way to get the base class version(s) is by explicitly asking for it.)
The extra method and setattr() don't seem to be gaining you anything.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #4
On Thu, 22 Jul 2004 10:58:33 -0700, Jeff Shannon <je**@ccvcorp.c om>
wrote:
[#drFrame is inherited from wx.Frame]
[#in drFrame there are some calls to SetStatusText]

I think, I don't make it clear enough.

in drFrame there are some calls to SetStatusText.
these, I want catch, change some strings, and send it back;
simply said.
What's the point of this extra 'NewStatusText' method? You're already
overriding SetStatusText. Without the NewStatusText and the setattr()
call, when someone calls SomePluginObjec t.SetStatusText (), it will call
your new version rather than the wx.Frame (or DrFrame) version

but not from drFrame itself; that is the point.

I want:
ALL the calls SetStatusText IN DRFRAME itself should be processed.
-- that's
the whole point of inheritance. (This is true even when the 'someone'
is the object itself, from within the DrFrame base class. The *only*
way to get the base class version(s) is by explicitly asking for it.)
The extra method and setattr() don't seem to be gaining you anything.


these calls (in drFrame), I want to "trick", to go further down one
level, or better said redirect to my own function, change the text,
and use the normal SetStatusText from wx.Frame.
Anyway, thank you.
--
Francesco
Jul 18 '05 #5
Francesco wrote:
What's the point of this extra 'NewStatusText' method? You're already
overriding SetStatusText. Without the NewStatusText and the setattr()
call, when someone calls SomePluginObjec t.SetStatusText (), it will call
your new version rather than the wx.Frame (or DrFrame) version


but not from drFrame itself; that is the point.


Unless I'm misunderstandin g your intent, then yes, from drFrame itself.

Every Plugin object is also a drFrame object (and a wx.Frame object).
*Every* SetStatusText() call on any such object will use the
most-derived version of SetStatusText() , i.e. the Plugin version. Even
the calls that are part of the drFrame class definition. The *only*
exception to this is if, instead of using self.SetStatusT ext(), you're
using drFrame.SetStat usText(self, ...) -- in that case, you've
explicitly asked for the drFrame version. But if you have, for example,
something like this:

class drFrame(wx.Fram e):
def do_something(se lf, *args):
# [....]
self.SetStatusT ext("New Status")

then despite the fact that this is a drFrame method, and doesn't know
anything about the Plugin derived class, it will indeed result in the
Plugin version of SetStatusText() being called. That's the whole
*point* of inheritance.

I can see two cases where what you're doing would have some effect that
doesn't replicate normal method resolution order, and to be honest I
think that using either case is a sign of some very questionable design
decisions and definitely violates the guiding principles of OO and
inheritance. One case would be that there are indeed places where the
drFrame.SetStat usText(self, ...) is explicitly called, and you're trying
to override those explicit requests. This is bad form -- an explicit
request should be honored, because there's probably a real reason for
it. The other case is where you are trying to modify the behavior of
all drFrame instances, including those which are *not* also Plugin
instances.

In both cases, you're doing something magic behind your back. It's much
better to do that kind of stuff up front, where it can be seen. (For
instance, in the second case, simply derive an intermediate class from
drFrame, and then derive Plugin from that. Use the intermediate class
anywhere where you'd currently use drFrame, and you're set.) I would
bet that there's a more straightforward way of accomplishing what you
actually want to do, without resorting to this kind of sleight-of-hand.
(I trust sleight-of-hand in programs about as much as I trust
sleight-of-hand in people who're holding my wallet -- there may be cases
where it's justified and where it can be trusted, but I'm not going to
hand my wallet to just anyone...)

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #6
On Thu, 22 Jul 2004 14:24:28 -0700, Jeff Shannon <je**@ccvcorp.c om>
wrote:

Hello Jeff,
Francesco wrote:
What's the point of this extra 'NewStatusText' method? You're already
overriding SetStatusText. Without the NewStatusText and the setattr()
call, when someone calls SomePluginObjec t.SetStatusText (), it will call
your new version rather than the wx.Frame (or DrFrame) version
but not from drFrame itself; that is the point.


Unless I'm misunderstandin g your intent, then yes, from drFrame itself.

Every Plugin object is also a drFrame object (and a wx.Frame object).


No. Plugin is a *function*, which receives a *drFrame object*.
It should have the purpose to "redirect" all the SetStatusText
calls from within drFrame.
*Every* SetStatusText() call on any such object will use the
most-derived version of SetStatusText() , i.e. the Plugin version. Even
the calls that are part of the drFrame class definition. The *only*
exception to this is if, instead of using self.SetStatusT ext(), you're
using drFrame.SetStat usText(self, ...) -- in that case, you've
explicitly asked for the drFrame version. But if you have, for example,
something like this:

class drFrame(wx.Fram e):
def do_something(se lf, *args):
# [....]
self.SetStatusT ext("New Status")

then despite the fact that this is a drFrame method, and doesn't know
anything about the Plugin derived class, it will indeed result in the
Plugin version of SetStatusText() being called. That's the whole
*point* of inheritance.
I see.

I can see two cases where what you're doing would have some effect that
doesn't replicate normal method resolution order, and to be honest I
think that using either case is a sign of some very questionable design
decisions and definitely violates the guiding principles of OO and
inheritance. One case would be that there are indeed places where the
drFrame.SetSta tusText(self, ...) is explicitly called, and you're trying
to override those explicit requests. This is bad form -- an explicit
request should be honored, because there's probably a real reason for
it. The other case is where you are trying to modify the behavior of
all drFrame instances, including those which are *not* also Plugin
instances.
This I want to achieve.

I betted, that there comes the question of bad design :)

In both cases, you're doing something magic behind your back. It's much
better to do that kind of stuff up front, where it can be seen. (For
instance, in the second case, simply derive an intermediate class from
drFrame, and then derive Plugin from that. Use the intermediate class
anywhere where you'd currently use drFrame, and you're set.) I would
bet that there's a more straightforward way of accomplishing what you
actually want to do, without resorting to this kind of sleight-of-hand.
(I trust sleight-of-hand in programs about as much as I trust
sleight-of-hand in people who're holding my wallet -- there may be cases
where it's justified and where it can be trusted, but I'm not going to
hand my wallet to just anyone...)


Thank you for your extensive explanation and for the tips.

I will think about it again.
--
Franz Steinhaeusler
Jul 18 '05 #7
Franz Steinhaeusler wrote:
On Thu, 22 Jul 2004 14:24:28 -0700, Jeff Shannon <je**@ccvcorp.c om>
wrote:
Unless I'm misunderstandin g your intent, then yes, from drFrame itself.

Every Plugin object is also a drFrame object (and a wx.Frame object).


No. Plugin is a *function*, which receives a *drFrame object*.
It should have the purpose to "redirect" all the SetStatusText
calls from within drFrame.


Ah, well, then I *was* misunderstandin g -- I'm not sure how I became
convinced that Plugin was a derived class, but convinced I was, and
built all further opinions on top of that false conviction.

I'd say that if you want a function to alter the behavior of all objects
of a particular class, it's probably better to have some sort of flag as
a class variable, and have your function set that flag rather than
rebind a method. I'd still be inclined to think that this should be
done on a per-instance basis, rather than for the entire class, but I
can see some case to be made for changing the entire class, too.

Perhaps you can define a StatusTextForma t attribute on drFrame, and have
drFrame.SetStat usText() apply that format to all text before feeding it
to wx.Frame.SetSta tusText(). Plugin() would then change this
StatusTextForma t. You could implement this as either a function to be
called, or use % string formatting...

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #8
On Fri, 23 Jul 2004 13:45:07 -0700, Jeff Shannon <je**@ccvcorp.c om>
wrote:
Ah, well, then I *was* misunderstandin g -- I'm not sure how I became
convinced that Plugin was a derived class, but convinced I was, and
built all further opinions on top of that false conviction.

I'd say that if you want a function to alter the behavior of all objects
of a particular class, it's probably better to have some sort of flag as
a class variable, and have your function set that flag rather than
rebind a method. I'd still be inclined to think that this should be
done on a per-instance basis, rather than for the entire class, but I
can see some case to be made for changing the entire class, too.

Perhaps you can define a StatusTextForma t attribute on drFrame, and have
drFrame.SetSta tusText() apply that format to all text before feeding it
to wx.Frame.SetSta tusText(). Plugin() would then change this
StatusTextForm at. You could implement this as either a function to be
called, or use % string formatting...

thank you again for your answer.
I have to think about it.

--
Franz Steinhaeusler
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
6949
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I guess I'll try to narrow it down to a few specific questions, but any further input offered on the subject is greatly appreciated: 1. Are all of my class's methods supposed to take 'self' as their first arg? 2. Am I then supposed to call...
4
2151
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data access module that creates a large disconnected dataset from a database. I want to pass that dataset back to the calling program. And then perhaps I want to send that dataset to another class module. At first it seems that the "object oriented"...
8
24271
by: Andreas Klemt | last post by:
Hello, I get this error Message "cannot redirect after http headers have been sent" when I do this response.redirect ("home.aspx") How can I find out with vb.net if already a http header has been sent like If response.IsSentHttpHeader then ....
3
3482
by: michael_vanommeren | last post by:
I have two web applications that I am working with and I am trying to do a Response.Redirect from one to the other. They are setup as separate web applications on the same IIS server. If I go directly to the second application in a browser, all of my events fire correctly. However, if I have the first application do a Response.Redirect to the second, for some reason none of my events on the second application ever get fired. After I...
8
2168
by: Mantorok | last post by:
Hi all When I start a new thread that tries to call: HttpContext.Current.Response.Redirect() It fails as Current returns null, is there anyway to access the current httpcontext from within a new thread? Thanks
1
2740
by: MikeM | last post by:
We are getting a behavior on a Response.Redirect("SomeUrl", True) that I'm hoping someone can explain. This all refers to the code snip at the end. By the way, this is all VB ASP.NET v1.0 code. So we have the Page_Load event of an ASPX page fire which in the snip you see calls "SomeMethod". Also you see in Page_Load two Catch statements. The first is "SpecialException". We created our own extended exception (inherits from...
2
2845
by: Peter McEvoy | last post by:
Folks, I've been building a Webservice API for a contract that will be exposed to the internet at large. There are two endpoints, and each endpoint contains a number of webmethods. Every webmethod returns a returncode which indicates the level of success in performing the operation (as well as the actual results of the call in an "out" parameter) They have a requirement that when they want to perform maintenance on the backend system,...
5
4569
by: venner | last post by:
I'm having an issue with an ASP.NET website after upgrading to ASP.NET 2.0. The website makes use of a central authentication service (CAS) provided at the university I work for. Each page checks a session variable, and if it is not present, does a Response.Redirect to a webpage for the CAS passing a url parameter for the url to post back to. The CAS provides a page for the user to log into, validates the username and password, and then...
1
7294
by: gnawz | last post by:
Hi guys, I have a couple of php files that perform various tasks. I will use fields in my system and provide code as well I need help as follows: My database contains the fields Category and Brand I need to make some changes on a a number of brands in a Category
0
8728
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,...
1
9130
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
9059
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8005
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...
1
6668
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4484
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4751
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3187
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
2
2572
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.