473,395 Members | 1,938 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.

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_status" should be then:
"mytext: hallo"

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

my_frame = Myframe

Plugin (my_frame)

def Plugin(frame_inst)

#here i want to redirect the setstatustext

setattr(frame_inst, 'SetStatusText', NewStatusText)

def SetStatusText(te,text,nr=0):
super(MyFrame, MyFrame).SetStatusText("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 1499
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(self):
SetStatusText ("hallo")
my_frame = Myframe perhaps you mean:
my_frame = Myframe() Plugin (my_frame) def Plugin(frame_inst) No colon above
#here i want to redirect the setstatustext
setattr(frame_inst, 'SetStatusText', NewStatusText) You seem to be trying to create a subclass dynamically.
def SetStatusText(te,text,nr=0):
super(MyFrame, MyFrame).SetStatusText("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.Frame):
def show_status(self):
SetStatusText("hallo")

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

-Scott David Daniels
Sc***********@Acm.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.Frame):
def show_status(self):
SetStatusText("hallo")

def SetStatusText(self, te, text, nr=0):
super(MyFrame, self).SetStatusText("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(text, 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(DrFrame), DrFrame).SetStatusText(text, col)

def NewStatusText(text, col=0):
SetStatusText(text, 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(text, col):
#[...]

def NewStatusText(text, col=0):
SetStatusText(text, 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 SomePluginObject.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.com>
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 SomePluginObject.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 SomePluginObject.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 misunderstanding 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.SetStatusText(), you're
using drFrame.SetStatusText(self, ...) -- in that case, you've
explicitly asked for the drFrame version. But if you have, for example,
something like this:

class drFrame(wx.Frame):
def do_something(self, *args):
# [....]
self.SetStatusText("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.SetStatusText(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.com>
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 SomePluginObject.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 misunderstanding 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.SetStatusText(), you're
using drFrame.SetStatusText(self, ...) -- in that case, you've
explicitly asked for the drFrame version. But if you have, for example,
something like this:

class drFrame(wx.Frame):
def do_something(self, *args):
# [....]
self.SetStatusText("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.SetStatusText(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.com>
wrote:
Unless I'm misunderstanding 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* misunderstanding -- 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 StatusTextFormat attribute on drFrame, and have
drFrame.SetStatusText() apply that format to all text before feeding it
to wx.Frame.SetStatusText(). Plugin() would then change this
StatusTextFormat. 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.com>
wrote:
Ah, well, then I *was* misunderstanding -- 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 StatusTextFormat attribute on drFrame, and have
drFrame.SetStatusText() apply that format to all text before feeding it
to wx.Frame.SetStatusText(). Plugin() would then change this
StatusTextFormat. 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
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...
4
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...
8
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...
3
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...
8
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...
1
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. ...
2
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...
5
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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
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.