为什么描述符不能定义为实例属性 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
f12998765

为什么描述符不能定义为实例属性

  •  
  •   f12998765 Aug 30, 2017 2683 views
    This topic created in 3166 days ago, the information mentioned may be changed or developed.
    如题,为什么实例属性的描述符不会自动调用描述符协议的 __get__()…这些方法?

    只能在类属性中定义描述符?
    5 replies    2017-09-11 20:55:43 +08:00
    fangzq
    1
    fangzq  
       Aug 31, 2017
    说一下我的理解。python 文档里说,实例对象调用描述符的过程是这样的:调用 b.x 时,实际是转化为 type(b).__dict__['x'].__get__(b, type(b))执行的。如果 x 是实例属性的话,那肯定在类的 __dict__ 里找不到了,也就无法自动进行调用了。
    keakon
        2
    keakon  
       Aug 31, 2017
    https://docs.python.org/3/howto/descriptor.html#invoking-descriptors

    For objects, the machinery is in object.__getattribute__() which transforms b.x into type(b).__dict__['x'].__get__(b, type(b)). The implementation works through a precedence chain that gives data descriptors priority over instance variables, instance variables priority over non-data descriptors, and assigns lowest priority to __getattr__() if provided. The full C implementation can be found in PyObject_GenericGetAttr() in Objects/object.c.

    For classes, the machinery is in type.__getattribute__() which transforms B.x into B.__dict__['x'].__get__(None, B).
    f12998765
        3
    f12998765  
    OP
       Aug 31, 2017 via Android
    @fangzq @keakon 感谢

    我的疑问就是转换这一步,为什么要为转换为 type(b).__dict__['x'].__get__(b, type(b)) ,而不是直接调用 b.__dict__['x'].__get__(b, type(b))

    把 x 定义为实例属性,肯定可以在 b 的 __dict__ 中找到,但是输出 b.x 时却不会触发 b.__dict__['x'].__get__(b, type(b)) 这样的调用

    我知道这样的转换是在 object.__getattribute__()/type .__getattribute__() 写好的,只是想不明白为什么这样,有点卡壳

    如果是为了数据描述符和非数据描述符的优先级,那区分它们有什么意义吗?

    你们有什么想法,可以点一点我,感谢

    或许我应该去看看 PEP
    keakon
        4
    keakon  
       Sep 2, 2017   1
    先想想对象和类调用时,为什么传给 __get__ 的参数要不一样。再想想类也是对象,如果要满足你的设定,__get__ 的参数就得一样了。
    f12998765
        5
    f12998765  
    OP
       Sep 11, 2017 via Android
    @keakon 谢谢
    About     Help     Advertise     Blog     API     FAQ     Solana     811 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 49ms UTC 20:32 PVG 04:32 LAX 13:32 JFK 16:32
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86