//网站根目录 var __ID_HOME__="#zj_home"; //生产环境设置为true,本地设置为false var __JS_DEBUG__= true; var __JS_APP__ = ""; var __JS_APP_HOME__ = "/"; var __JS_PUBLIC__ = "/Public"; var __JS_HOME__ = "/Template/default/Home/Public"; var _URL_SIGN = "https://zhangjunbk.com:443/sign"; var _URL_LOGIN = "https://zhangjunbk.com:443/login"; var _URL_REG = "https://zhangjunbk.com:443/reg"; var _URL_FORGET = "https://zhangjunbk.com:443/forget"; var _URL_SEND_EMAIL = "https://zhangjunbk.com:443/sendEmail"; var _URL_SEND_MOBILE = "https://zhangjunbk.com:443/sendMobile"; var _URL_RELATION_EMAIL = "https://zhangjunbk.com:443/relationEmail"; var _URL_SIGN_RELATION_EMAIL = "https://zhangjunbk.com:443/sign_relation"; var _URL_FRONT_LOGOUT="/frontlogout"; var _URL_CHECK_LOGIN = "https://zhangjunbk.com:443/check_login"; var _URL_PAY_POINTS = "https://zhangjunbk.com:443/payPoints"; var _URL_PAY_POINTS_VIDEO = "https://zhangjunbk.com:443/payPointsVideo"; var _URL_USER_POINTS = "https://zhangjunbk.com:443/uindex"; var _URL_AJAX_STOCK_SEALING = "https://zhangjunbk.com:443/ajaxStockSealing"; var _URL_API_WX_CREATE = "https://zhangjunbk.com:443/wxV2Create"; var _URL_API_WX_CREATE_NATIVE_REWARD = "https://zhangjunbk.com:443/wxV2CreateNative_reward"; var _URL_CENTER_USER = "https://zhangjunbk.com:443/centerUser"; var _URL_QQ = "{https://www.zhangjunbk.com/oauth/callback/}";

python的描述符(descriptor)、装饰器(property)造成

系统 1673 0

分享一下刚遇到的一个小问题,我有一段类似于这样的python代码:

复制代码 代码如下:

# coding: utf-8

class A(object):

    @property
    def _value(self):
#        raise AttributeError("test")
        return {"v": "This is a test."}

    def __getattr__(self, key):
        print "__getattr__:", key
        return self._value[key]

if __name__ == '__main__':
    a = A()
    print a.v


运行后可以得到正确的结果
复制代码 代码如下:

__getattr__: v
This is a test.

但是注意,如果把
复制代码 代码如下:

#        raise AttributeError("test")


这行的注释去掉的话,即在_value方法里面抛出AttributeError异常,事情就会变得有些奇怪。程序运行的时候并不会抛出异常,而是会进入一个无限递归:

复制代码 代码如下:

File "attr_test.py", line 12, in __getattr__
    return self._value[key]
  File "attr_test.py", line 12, in __getattr__
    return self._value[key]
RuntimeError: maximum recursion depth exceeded while calling a Python object

通过多方查找后发现是property装饰器的问题,property实际上是一个descriptor。在python doc中可以发现这样的文字:

复制代码 代码如下:

object.__get__(self, instance, owner)

Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). owner is always the owner class, while instance is the instance that the attribute was accessed through, or None when the attribute is accessed through the owner. This method should return the (computed) attribute value or raise an AttributeError exception.

这样当用户访问._value时,抛出了AttributeError从而调用了__getattr__方法去尝试获取。这样程序就变成了无限递归。

这个问题看上去不复杂,但是当你的_value方法是比较隐晦的抛出AttributeError的话,调试起来就会比较困难了。


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论