V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2016-07-05 22:14:54 +08:00 · 2523 次点击
    这是一个创建于 2860 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2016-07-06 13:45:15 +08:00
    sudo987
        1
    sudo987  
    OP
       2016-07-05 22:19:45 +08:00
    发完贴就明白了,__getattribute__里调用 getattr 会重新调用外层方法的__getattribute__, 从而造成死循环。
    strahe
        2
    strahe  
       2016-07-05 23:15:08 +08:00
    我今天下午刚碰到这个问题....
    sudo987
        3
    sudo987  
    OP
       2016-07-05 23:17:30 +08:00
    @strahe 刚才以为明白了,其实还是之前的问题,不懂为什么会出现死循环,我又发了个帖子, http://v2ex.com/t/290524#reply0
    sudo987
        4
    sudo987  
    OP
       2016-07-05 23:18:25 +08:00
    @strahe 你好,你有联系方式么?我想知道你是怎么理解的。
    strahe
        5
    strahe  
       2016-07-05 23:24:43 +08:00
    你不是都说出来了吗?每次引用方法或者属性(除特殊方法外)都会调用"__getattribute__",你调用 self.__obj,本身就是在引用它的一个属性
    shyling
        6
    shyling  
       2016-07-05 23:26:13 +08:00 via Android
    self.__obj
    sudo987
        7
    sudo987  
    OP
       2016-07-05 23:27:19 +08:00
    @strahe 但是 getattr(self.__obj.attr)相当于 self.__obj.attr ,是调用 self.__obj 自身的__getattribute__,和 class A 的__getattribute__不是一个东西,所以我觉得不应该出现死循环。
    sudo987
        8
    sudo987  
    OP
       2016-07-05 23:34:24 +08:00
    @strahe 啊。。。明白了,问题不在 self.__obj.attr 去的 attr 上,而是在 self 取得__obj 上,在这里出现了死循环,谢谢。
    ruanimal
        9
    ruanimal  
       2016-07-06 08:34:48 +08:00
    自定义了__getattribute__,所有属性和字典访问都会通过自定义的__getattribute__,会死循环的。。
    nimdanoob
        10
    nimdanoob  
       2016-07-06 10:43:23 +08:00
    可能 没太仔细看吧,很好理解
    kaneg
        11
    kaneg  
       2016-07-06 13:45:15 +08:00
    根据楼主提供的代码,我觉得你想达到的效果是用 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)
    '''
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   761 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 22:26 · PVG 06:26 · LAX 15:26 · JFK 18:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.