修改 python 可调用函数,使其调用 before() ,然后调用实际函数 after()

发布于 2024-09-12 00:55:33 字数 664 浏览 9 评论 0原文

我不确定这是否是在函数 f1() 周围调用 beforeafter 函数的最佳方式。

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)

请指教。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-09-19 00:55:33

我会使用装饰器,如下所示:

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)

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)
云淡月浅 2024-09-19 00:55:33

您可以在 contextlib 模块中使用 @contextmanager 装饰器以及 with 语句来实现以下目的:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文