470,849 Members | 686 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Using descriptors to wrap methods

Here is a stripped-down version of a Python Cookbook recipe. Is there a
simpler, more Pythonical, natural way of doing this?

------
#! /usr/bin/env python

# Modified from Python Cookbook entry 91192, "eiffelmethod" by Andres
# Tuells. The url is
# http://aspn.activestate.com/ASPN/Coo...n/Recipe/91192

class MethodWraper(object):
def __init__(self, method):
self.method = method
def __get__(self, inst, type=None):
result = wrapper(inst, self.method)
setattr(inst, self.method.__name__, result)
return result

class wrapper:
def __init__(self, inst, method):
self.instance = inst
self.method = method

def __call__(self, *args, **kargs):
print 'pre'
result = apply(self.method, (self.instance,) + args, kargs)
print 'post'
return result

def test():
class C:
def f(self, arg):
print 'in f'
return arg+1
f = MethodWraper(f)

c = C()
print c.f(1)

if __name__=='__main__':
test()
------
Jul 18 '05 #1
2 1344
"Edward C. Jones" <ed******@erols.com> wrote in
news:40***********************@news.rcn.com:
Here is a stripped-down version of a Python Cookbook recipe. Is there a
simpler, more Pythonical, natural way of doing this?


Here's a simpler way of doing the same thing:
def MethodWrapper(f): def wrapper(self, *args, **kw):
print 'pre'
result = f(self, *args, **kw)
print 'post'
return result
return wrapper
class C(object): def f(self, arg):
print 'in f'
return arg+1
f = MethodWrapper(f)

c = C()
print c.f(1) pre
in f
post
2
Or if you want pre and post code customisable you might try:
def MethodWrapper(f, pre=None, post=None): def wrapper(self, *args, **kw):
if pre: pre(self, *args, **kw)
result = f(self, *args, **kw)
if post: post(self, result, *args, **kw)
return result
return wrapper
class C(object): def pre_f(self, arg):
print 'pre',arg
def post_f(self, res, arg):
print 'post',res,arg
def f(self, arg):
print 'in f'
return arg+1
f = MethodWrapper(f, pre_f, post_f)
c = C()
c.f(1) pre 1
in f
post 2 1
2


I don't know if you could call it Pythonic though.
Jul 18 '05 #2

"Edward C. Jones" <ed******@erols.com> wrote in message
news:40***********************@news.rcn.com...
Here is a stripped-down version of a Python Cookbook recipe. Is there a
simpler, more Pythonical, natural way of doing this?


def wrap(f):
def wrapped(*args, **kwds):
print 'pre'
result = f(*args, **kwds)
print 'post'
return result
return wrapped
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Denis S. Otkidach | last post: by
9 posts views Thread by Lenard Lindstrom | last post: by
reply views Thread by Steven Bethard | last post: by
12 posts views Thread by bruno at modulix | last post: by
3 posts views Thread by redefined.horizons | last post: by
2 posts views Thread by DJ Dharme | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.