gpt4 book ai didi

python 装饰器代理不起作用

转载 作者:行者123 更新时间:2023-11-30 23:17:02 26 4
gpt4 key购买 nike

我尝试创建一个跟踪装饰器来跟踪调用了哪个方法。让我们先阅读代码。代码是用python 2.7编写的

class Decor(object):
def __init__(self,cls):
self.cls=cls

def __call__(self,*arg):
self.instance = self.cls(*arg)
return self

def __getattr__(self,name):
print "attribute name of instance of class {} = {}".format(self.cls.__name__,name)
return getattr(self.instance,name)

@Decor
class Foo(object):
attr1 = 10
def __init__(self,value):
self.value = value

def method1(self):
self.method2()
print self
print "value={}".format(self.value)

def method2(self):
print "hi"


f=Foo(10)
print f
f.method1()

输出:

1. <__main__.Decor object at 0x02758B90>
2. attribute name of instance of class Foo = method1
3. hi
4. <__main__.Foo object at 0x02758C30>
5. value=10

我预计 method2 会像 method1 那样打印。你能解释一下为什么method2没有得到详细的打印吗?

我不明白输出第4行。它显示 Foo 对象而不是 Decor 对象。 f 是一个 Decor 对象,为什么 self 是一个 Foo 对象?请解释一下!

请给我一个解决这个问题的建议。谢谢

最佳答案

这里的问题是 Foo.instance1 中的 self 是一个 Foo 实例,而不是 Decor实例。

如果打印出 f.method1 本身,您可以看到这一点:它是一个实例为 f 的绑定(bind)方法:

>>> f.method1
attribute name of instance of class Foo = method1
<bound method Foo.method1 of <__main__.Foo object at 0x10c641e90>>
为什么?好吧,您返回了 getattr(self.instance, 'method1'),它与 self.instance.method1 执行相同的操作,因此它是 self.instance 被烘焙到绑定(bind)方法中。

因此,当 method1 代码查找 self.method2 (或 self.value)时,它会在 >Foo 实例,因此您的代码不会运行。

如果您不明白这一点,请尝试阅读 How Methods Work 。 (如果您不想接受属性查找的神奇之处,请点击末尾的链接 Descriptor HowTo Guide。)

<小时/>

因此,如果您希望 self 成为 instance1 内的 Decor 实例,则必须使用 返回一个绑定(bind)方法Decor 实例,而不是 Foo 实例。您可以通过使用 types.MethodType 手动创建一个来做到这一点。 (或者,一旦了解了描述符,您可以直接查找函数实现,然后手动调用其 __get__。)

例如:

def __getattr__(self, name):
print "attribute name of instance of class {} = {}".format(self.cls.__name__,name)
attr = getattr(self.instance, name)
if callable(attr):
return types.MethodType(attr.__func__, self, type(self))
else:
return attr

(如果您希望它能够与类方法、静态方法、非方法可调用对象(例如存储在实例字典中的函数等)一起使用,当然,它需要比仅仅 callable 更复杂的东西。 )

现在:

>>> f.method1
attribute name of instance of class Foo = method1
Out[26]: <bound method Decor.method1 of <__main__.Decor object at 0x10c7e9f10>>

所以,当我们调用它时:

>>> f.method1()
attribute name of instance of class Foo = method1
attribute name of instance of class Foo = method2
hi
<__main__.Decor object at 0x10c7e9f10>
attribute name of instance of class Foo = value
value=10

关于python 装饰器代理不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27455004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com