如何拦截实例方法调用?

发布于 2024-10-12 21:32:11 字数 723 浏览 7 评论 0原文

我正在寻找一种方法来拦截下面类 MyWrapper 中的实例方法调用:

class SomeClass1:
    def a1(self):
        self.internal_z()
        return "a1"
    def a2(self):
        return "a2"
    def internal_z(self):
        return "z"

class SomeClass2(SomeClass1):
    pass

class MyWrapper(SomeClass2):

    # def INTERCEPT_ALL_FUNCTION_CALLS():
    #      result = Call_Original_Function()
    #      self.str += result  
    #      return result  


    def __init__(self):
        self.str = ''
    def getFinalResult(self):
        return self.str

x = MyWrapper()
x.a1()
x.a2()

我想拦截通过我的包装类进行的所有函数调用。在我的包装类中,我想跟踪所有结果字符串。

result = x.getFinalResult()
print result == 'a1a2'

I am looking for a way to intercept instance method calls in class MyWrapper below:

class SomeClass1:
    def a1(self):
        self.internal_z()
        return "a1"
    def a2(self):
        return "a2"
    def internal_z(self):
        return "z"

class SomeClass2(SomeClass1):
    pass

class MyWrapper(SomeClass2):

    # def INTERCEPT_ALL_FUNCTION_CALLS():
    #      result = Call_Original_Function()
    #      self.str += result  
    #      return result  


    def __init__(self):
        self.str = ''
    def getFinalResult(self):
        return self.str

x = MyWrapper()
x.a1()
x.a2()

I want to intercept all function calls make through my wrapper class. In my wrapper class I want to keep track of all the result strings.

result = x.getFinalResult()
print result == 'a1a2'

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

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

发布评论

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

评论(3

丶情人眼里出诗心の 2024-10-19 21:32:11

一些快速而肮脏的代码:

class Wrapper:
    def __init__(self, obj):
        self.obj = obj
        self.callable_results = []

    def __getattr__(self, attr):
        print("Getting {0}.{1}".format(type(self.obj).__name__, attr))
        ret = getattr(self.obj, attr)
        if hasattr(ret, "__call__"):
            return self.FunctionWrapper(self, ret)
        return ret

    class FunctionWrapper:
        def __init__(self, parent, callable):
            self.parent = parent
            self.callable = callable

        def __call__(self, *args, **kwargs):
            print("Calling {0}.{1}".format(
                  type(self.parent.obj).__name__, self.callable.__name__))
            ret = self.callable(*args, **kwargs)
            self.parent.callable_results.append(ret)
            return ret

class A:
    def __init__(self, val): self.val = val
    def getval(self): return self.val

w = Wrapper(A(10))
print(w.val)
w.getval()
print(w.callable_results)

可能不彻底,但我想可能是一个不错的起点。

Some quick and dirty code:

class Wrapper:
    def __init__(self, obj):
        self.obj = obj
        self.callable_results = []

    def __getattr__(self, attr):
        print("Getting {0}.{1}".format(type(self.obj).__name__, attr))
        ret = getattr(self.obj, attr)
        if hasattr(ret, "__call__"):
            return self.FunctionWrapper(self, ret)
        return ret

    class FunctionWrapper:
        def __init__(self, parent, callable):
            self.parent = parent
            self.callable = callable

        def __call__(self, *args, **kwargs):
            print("Calling {0}.{1}".format(
                  type(self.parent.obj).__name__, self.callable.__name__))
            ret = self.callable(*args, **kwargs)
            self.parent.callable_results.append(ret)
            return ret

class A:
    def __init__(self, val): self.val = val
    def getval(self): return self.val

w = Wrapper(A(10))
print(w.val)
w.getval()
print(w.callable_results)

Might not be thorough, but could be a decent starting point, I guess.

╄→承喏 2024-10-19 21:32:11

您可以在实例化时间内使用装饰器包装您的方法:

#!/usr/bin/env python

import inspect

def log(func):
    def _logged(*args, **kw):
        print "[LD] func", func.__name__, "called with:", args, kw
        result = func(*args, **kw)
        print "[LD] func", func.__name__, "returned:", result
        return result
    return _logged

class A(object):
    def __init__(self):
        for x in inspect.getmembers(self, (inspect.ismethod)):
            if not x[0].startswith('__'):
                setattr(self, x[0], log(getattr(self, x[0])))

    def hello(self):
        print "Hello"

    def bye(self):
        print "Bye"
        return 0

现在,如果您调用 hellobye,则该调用首先会经过 log

a = A()
a.hello()
a.bye()

# [LD] func hello called with: () {}
# Hello
# [LD] func hello returned: None
# [LD] func bye called with: () {}
# Bye
# [LD] func bye returned: 0

You could wrap your methods with decorators a instanciation time:

#!/usr/bin/env python

import inspect

def log(func):
    def _logged(*args, **kw):
        print "[LD] func", func.__name__, "called with:", args, kw
        result = func(*args, **kw)
        print "[LD] func", func.__name__, "returned:", result
        return result
    return _logged

class A(object):
    def __init__(self):
        for x in inspect.getmembers(self, (inspect.ismethod)):
            if not x[0].startswith('__'):
                setattr(self, x[0], log(getattr(self, x[0])))

    def hello(self):
        print "Hello"

    def bye(self):
        print "Bye"
        return 0

Now if you call hello or bye, the call goes through log first:

a = A()
a.hello()
a.bye()

# [LD] func hello called with: () {}
# Hello
# [LD] func hello returned: None
# [LD] func bye called with: () {}
# Bye
# [LD] func bye returned: 0
哭泣的笑容 2024-10-19 21:32:11

您想要做的与这个问题非常相似。
您应该以相反的顺序获取示例代码,我的意思是创建一个类来记录方法调用的返回值,并使您想要观看的类继承它。
这将给出类似这样的内容

class RetValWatcher(object):
    def __init__(self):
        self.retvals = []

    def __getattribute__(self, name):
        attr = super(RetValWatcher, self).__getattribute__(name)
        if callable(attr):
            def wrapped(*args, **kwargs):
                retval = attr(*args, **kwargs)
                self.retvals.append(retval)
                return retval
            return wrapped
        else:
            return attr

    def getFinalResult(self):
        return ''.join(self.retvals)

class MyClass(RetValWatcher):
    def a(self):
        self.internal_z()
        return 'a1'

    def b(self):
        return 'b1'

    def internal_z(self):
        return 'z'

x = MyClass()
x.a()
x.b()
print x.getFinalResult()
#'za1b1'

通过一些小的更改,此方法还允许您记录所有 RetValWatcher 实例的返回值。

编辑:添加了奇点评论建议的更改

编辑2:忘记处理 attr 不是方法的情况(再次感谢奇点)

编辑3:修复了拼写错误

What you want to do is quite similar to this question.
You should take your example code in the reverse order, i mean creating a class to record return values of method calls, and make the classes you want to watch inherit from it.
Which would give something like this

class RetValWatcher(object):
    def __init__(self):
        self.retvals = []

    def __getattribute__(self, name):
        attr = super(RetValWatcher, self).__getattribute__(name)
        if callable(attr):
            def wrapped(*args, **kwargs):
                retval = attr(*args, **kwargs)
                self.retvals.append(retval)
                return retval
            return wrapped
        else:
            return attr

    def getFinalResult(self):
        return ''.join(self.retvals)

class MyClass(RetValWatcher):
    def a(self):
        self.internal_z()
        return 'a1'

    def b(self):
        return 'b1'

    def internal_z(self):
        return 'z'

x = MyClass()
x.a()
x.b()
print x.getFinalResult()
#'za1b1'

With some minor changes, this method would also allow you to record return values across all RetValWatcher instances.

Edit: added changes suggested by singularity's comment

Edit2: forgot to handle the case where attr is not a method (thx singularity again)

Edit3: fixed typo

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