How Can I overide a function in an existing class
Peter Hansen
peter at engcorp.com
Tue Sep 14 21:08:42 EDT 2004
Eddie wrote:
> I have changed the BaseHTTPServer.py file to do what I want and
> included it in the directory with my service. This works, but there
> has to be a better way.
>
> Is there some other way to override the function?
Often I've found it possible and relatively "clean" to write
code that modifies the class involved dynamically, often
just wrapping the existing method in another one that does
what I want (or undoes it, perhaps), or just by sticking
in a replacement.
Trivialized example that I hope gives you the idea:
# file BaseHTTPServer.py
class SomeClass:
def someMethod(self):
log_message(somemsg) # undesirable call
# other stuff that is good
# in your own file
import BaseHTTPServer
def replacementMethod(self):
# only the good stuff
BaseHTTPServer.SomeClass.someMethod = replacementMethod
# followed by the rest of your existing code that
# actually instantiates a SomeClass, even if indirectly
Any help?
-Peter
More information about the Python-list
mailing list