如何拦截实例方法调用?

时间:2011-01-18 11:52:03

标签: python function call instance getattr

我正在寻找一种方法来拦截下面的类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'

3 个答案:

答案 0 :(得分:5)

一些快速而肮脏的代码:

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)

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

答案 1 :(得分:2)

你可以用装饰器包装你的方法一个实例时间:

#!/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

答案 2 :(得分:2)

您想要做的与this question非常相似。 您应该以相反的顺序获取示例代码,我的意思是创建一个类来记录方法调用的返回值,并使您想要监视的类继承它。  哪个会有这样的东西

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 getFinalRestult(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实例中记录返回值。

编辑:添加了由singularity的评论

建议的更改

Edit2:忘了处理attr不是方法的情况(thx奇点再次)