如何在没有无限递归错误的情况下实现 __getattribute__? [英] How do I implement __getattribute__ without an infinite recursion error?

查看:34
本文介绍了如何在没有无限递归错误的情况下实现 __getattribute__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖对类中一个变量的访问,但正常返回所有其他变量.我如何使用 __getattribute__ 完成此操作?

I want to override access to one variable in a class, but return all others normally. How do I accomplish this with __getattribute__?

我尝试了以下操作(这也应该说明我正在尝试做的事情),但出现递归错误:

I tried the following (which should also illustrate what I'm trying to do) but I get a recursion error:

class D(object):
    def __init__(self):
        self.test=20
        self.test2=21
    def __getattribute__(self,name):
        if name=='test':
            return 0.
        else:
            return self.__dict__[name]

>>> print D().test
0.0
>>> print D().test2
...
RuntimeError: maximum recursion depth exceeded in cmp

推荐答案

您收到递归错误,因为您尝试访问 __getattribute__ 内的 self.__dict__ 属性会调用您的__getattribute__ 再次.如果您使用 object__getattribute__ 代替,它会起作用:

You get a recursion error because your attempt to access the self.__dict__ attribute inside __getattribute__ invokes your __getattribute__ again. If you use object's __getattribute__ instead, it works:

class D(object):
    def __init__(self):
        self.test=20
        self.test2=21
    def __getattribute__(self,name):
        if name=='test':
            return 0.
        else:
            return object.__getattribute__(self, name)

这是可行的,因为 object(在本例中)是基类.通过调用 __getattribute__ 的基本版本,您可以避免之前遇到的递归地狱.

This works because object (in this example) is the base class. By calling the base version of __getattribute__ you avoid the recursive hell you were in before.

带有 foo.py 代码的 Ipython 输出:

Ipython output with code in foo.py:

In [1]: from foo import *

In [2]: d = D()

In [3]: d.test
Out[3]: 0.0

In [4]: d.test2
Out[4]: 21

更新:

在标题为 的部分中有一些内容当前文档中对新样式类的更多属性访问,他们建议完全这样做以避免无限递归.

There's something in the section titled More attribute access for new-style classes in the current documentation, where they recommend doing exactly this to avoid the infinite recursion.

这篇关于如何在没有无限递归错误的情况下实现 __getattribute__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆