Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
描述符和python提供的属性_Python_Descriptor_New Style Class - Fatal编程技术网

描述符和python提供的属性

描述符和python提供的属性,python,descriptor,new-style-class,Python,Descriptor,New Style Class,我正在学习Python,并试图更好地理解描述符。当我看这本Python在线书籍时,它说: 如果attrname是objectname的一个特殊(即Python提供的)属性,则返回它 我不明白Python提供的是什么意思。有人能给我一个Python提供的属性的例子,它优先于通常的解析顺序吗 注意:我只对新样式类感兴趣(据我所知,描述符甚至不适用于旧样式) >>> class Test(object): __dict__ = {'__class__' : "dict of

我正在学习Python,并试图更好地理解描述符。当我看这本Python在线书籍时,它说:

  • 如果attrname是objectname的一个特殊(即Python提供的)属性,则返回它
  • 我不明白Python提供的是什么意思。有人能给我一个Python提供的属性的例子,它优先于通常的解析顺序吗


    注意:我只对新样式类感兴趣(据我所知,描述符甚至不适用于旧样式)

    >>> class Test(object):
        __dict__ = {'__class__' : "dict of Test"}
        def __init__(self):
            self.__dict__['__class__'] = "dict of test"
    
    
    >>> test = Test()
    >>> test.__class__
    <class '__main__.Test'>
    >>> test.__dict__
    {'__class__': 'dict of test'}
    >>> Test.__dict__
    dict_proxy({'__dict__': {'__class__': 'dict of test'}, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'Test' objects>, '__doc__': None, '__init__': <function __init__ at 0x02BD2770>})
    >>> 
    
    根据对象的类型,还有更多的特殊属性名称。 例如,功能:

    >>> def test():pass
    
    >>> dir(test)
    ['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
    >>> test.func_closure
    >>> test.__dict__['func_closure']='roflcopter'
    >>> test.func_closure
    >>> test.__dict__['foo']='bar'
    >>> test.foo
    'bar'
    

    请参阅以获取概述

    正如您所猜测的,步骤1完全错误并且不存在。

    我以前没有意识到这一点,但是直接修改类的
    \uuu dict\uuu
    似乎不适用于新样式的类-它会导致
    类型错误:“dict\u proxy”对象不支持项分配
    。通过修改实例的
    \uuu dict\uuu
    ,即
    测试,您仍然可以在python 3中看到上述行为。另外,您知道还有哪些其他属性是这样工作的吗?我发现的另一个问题是
    \uuuu dict\uuu
    本身。我的问题是关于新样式的类,您演示的仅适用于旧样式。实际上,这是正常的解析顺序。它按照预期从对象字典中的数据描述符中获取值<代码>测试>测试=测试()测试=测试()测试(代码>代码>测试)测试(代码>测试)测试(代码>测试)测试(代码>测试)测试(代码>测试(代码>测试)测试=测试(代码>测试)测试(代码>测试=测试)测试(代码>测试)测试(代码>测试)测试(代码>测试)测试)测试(代码>测试)测试(测试)测试)在类中寻找一个描述符,在类中寻找一个描述符,在类中寻找一个描述符,在类中寻找一个描述符,在类中寻找一个描述符,在类中寻找一个描述符,在类中,在类中,在类中,在类中,在类中(UUUuuuuuuuuuuuuuuuuuuudididididididididic.....UU:伪:伪:伪:伪:伪:伪:伪:伪:伪:伪:伪:伪:伪:伪(伪_u类.\uuu set\uuuu)
    您的示例并没有表明它受到了不同的对待。现在我很困惑。签出:
    >>类测试(对象):\uuu类>>Test.\uuuuu class\uuuuuu
    我现在有点困惑,这似乎与这本书相矛盾,因为如果
    \uuuu class\uuuuuuuu
    不是特别的,那么当objname有一个非class
    \uuuu class\uuuuuuuuu>时,代码查找
    objectname.\uuuuuuu class\uuuuuuuu>会如何进行?在我看来,
    \uuuuuu类
    必须是特殊的,才能让这个算法工作。
    >>> test.__dict__ = {'__lolcat__': "bar"}
    >>> test.__lolcat__
    'bar'
    
    >>> def test():pass
    
    >>> dir(test)
    ['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
    >>> test.func_closure
    >>> test.__dict__['func_closure']='roflcopter'
    >>> test.func_closure
    >>> test.__dict__['foo']='bar'
    >>> test.foo
    'bar'