I'm trying to create an object with a run method that will be wrapped by a _wrap_run method
. I'd like to be able to call the method and it's wrapper by simply typing instance.run()
and I'd like to be able to subclass the object so I can override the run()
method and have it still execute the wrapper.
More simply put, I want people to be able to subclass A and override run()
but still have calls to the run()
method execute the wrapper function.
I'm having some difficulty with the mechanics of this. Does anyone have any suggestions regarding this approach?
class A:
def run(self):
print "Run A"
return True
def _wrap_run(self):
print "PRE"
return_value = self.run()
print "POST"
return return_value
run = property(_wrap_run)
a = A()
a.run()
"""
Should Print:
PRE
Run A
POST
"""
class B(A):
def run(self):
print "Run B"
return True
b = B()
b.run()
"""
Should Print:
PRE
Run B
POST
"""
_wrap_run
? Is this supposed to be some kind of decorator? What's wrong with using a decorator for this? – Downhill