推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
sudo987
V2EX  ›  Python

关于包装类和__getattribute__混用时出现的问题。

  •  1
     
  •   sudo987 · Jul 5, 2016 · 3274 views
    This topic created in 3623 days ago, the information mentioned may be changed or developed.
    class A(object):
        def __init__(self, obj):
            self.__obj = obj
        def __str__(self):
            return str(self.__obj)
        __repr__ = __str__
        def __getattribute__(self, attr):
            return getattr(self.__obj, attr)
    

    A().append(1) #RuntimeError: maximum recursion depth exceeded while calling a Python object

    已知:

    1. 如果在__getattribute__里执行的是诸如 return self.attr 这样的操作,肯定会造成死循环,因为每一次执行 self.attr 都会调用外层的__getattribute__方法,然后一直循环。

    不解:

    1. 上面给出的代码在__getattribute__里面执行的是返回 self.__obj 中的 attr 也就是说执行的是 self.__obj.attr ,跟外层的__getattribute__是两个完全不同的同名方法,并不冲突,不明白为什么还是出现了死循环。
    11 replies    2016-07-06 13:45:15 +08:00
    sudo987
        1
    sudo987  
    OP
       Jul 5, 2016
    发完贴就明白了,__getattribute__里调用 getattr 会重新调用外层方法的__getattribute__, 从而造成死循环。
    strahe
        2
    strahe  
       Jul 5, 2016
    我今天下午刚碰到这个问题....
    sudo987
        3
    sudo987  
    OP
       Jul 5, 2016
    @strahe 刚才以为明白了,其实还是之前的问题,不懂为什么会出现死循环,我又发了个帖子, http://v2ex.com/t/290524#reply0
    sudo987
        4
    sudo987  
    OP
       Jul 5, 2016
    @strahe 你好,你有联系方式么?我想知道你是怎么理解的。
    strahe
        5
    strahe  
       Jul 5, 2016
    你不是都说出来了吗?每次引用方法或者属性(除特殊方法外)都会调用"__getattribute__",你调用 self.__obj,本身就是在引用它的一个属性
    shyling
        6
    shyling  
       Jul 5, 2016 via Android
    self.__obj
    sudo987
        7
    sudo987  
    OP
       Jul 5, 2016
    @strahe 但是 getattr(self.__obj.attr)相当于 self.__obj.attr ,是调用 self.__obj 自身的__getattribute__,和 class A 的__getattribute__不是一个东西,所以我觉得不应该出现死循环。
    sudo987
        8
    sudo987  
    OP
       Jul 5, 2016
    @strahe 啊。。。明白了,问题不在 self.__obj.attr 去的 attr 上,而是在 self 取得__obj 上,在这里出现了死循环,谢谢。
    ruanimal
        9
    ruanimal  
       Jul 6, 2016
    自定义了__getattribute__,所有属性和字典访问都会通过自定义的__getattribute__,会死循环的。。
    nimdanoob
        10
    nimdanoob  
       Jul 6, 2016
    可能 没太仔细看吧,很好理解
    kaneg
        11
    kaneg  
       Jul 6, 2016
    根据楼主提供的代码,我觉得你想达到的效果是用 A 来代理 obj ,覆盖__getattr__而不是__getattribute__可以达到此类效果:
    '''python
    class A(object):
    def __init__(self, obj):
    super(A, self).__init__()
    self.obj = obj

    def __getattr__(self, item):
    if item == 'obj':
    return self.obj
    else:
    return self.obj.__getattribute__(item)
    '''
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1213 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 17:48 · PVG 01:48 · LAX 10:48 · JFK 13:48
    ♥ Do have faith in what you're doing.