views:

85

answers:

2

Hello

I am not sure if this is the best way to have before and after functions be called around a function f1().

class ba(object):
    def __init__(self, call, before, after):
        self.call = call
        self.before = before
        self.after = after

    def __call__(self, *args):
        self.before()
        r = self.call(*args)
        self.after()
        return r


class test1(object):
    def mybefore(self):
        print "before func call"

    def myafter(self):
        print "after func call"

def meth1(a1, a2):
    print "meth1(a1=%d, a2=%d)" % (a1, a2)

t = test1()

wmeth1 = ba(meth1, t.mybefore, t.myafter)

wmeth1(1, 2)

Please advise.

+9  A: 

I'd use a decorator, like so:

from functools import wraps

class withBeforeAfter(object):
    def __init__(self, before, after):
        self.before = before
        self.after = after
    def __call__(self, wrappedCall):
        @wraps(wrappedCall)
        def wrapCall(*args, **kwargs):
            try:
                self.before()
                r = wrappedCall(*args, **kwargs)
            finally:
                self.after()
            return r
        return wrapCall

 # to use it:
 @withBeforeAfter(myBefore, myAFter)
 def myFunc(...):
     ...

 # or:
 def myFunc(...):
     ...
 # later...
 myFunc = withBeforeAfter(myBefore, myAfter)(myFunc)
Walter Mundt
How's that? Good names are hard, you know!
Walter Mundt
+1 good decorator name
msw
I am getting:Traceback (most recent call last): File "C:\Temp\4.py", line 35, in <module> myFunc = withBeforeAfter(myBefore, myAfter)(myFunc)TypeError: object.__new__() takes no parametersI tried with 2.5, 2.7 and 3.1
lallous
Oops, was missing a couple of underscores around the init method. Edited to fix.
Walter Mundt
Oops, I did not notice that too! Thanks!
lallous
+2  A: 

You could use the @contextmanager decorator in the contextlib module along with a with statement for something along these lines:

from contextlib import contextmanager

class test1(object):
    def mybefore(self):
        print "before func call"
    def myafter(self):
        print "after func call"

@contextmanager
def wrapper(cls):
    test = cls()
    test.mybefore()
    try:
        yield
    finally:
        test.myafter()

def f1(a1, a2):
    print "f1(a1=%d, a2=%d)" % (a1, a2)

with wrapper(test1):
    f1(1, 2)
martineau