<tfoot id='5skNy'></tfoot>
    • <bdo id='5skNy'></bdo><ul id='5skNy'></ul>

    <small id='5skNy'></small><noframes id='5skNy'>

    <i id='5skNy'><tr id='5skNy'><dt id='5skNy'><q id='5skNy'><span id='5skNy'><b id='5skNy'><form id='5skNy'><ins id='5skNy'></ins><ul id='5skNy'></ul><sub id='5skNy'></sub></form><legend id='5skNy'></legend><bdo id='5skNy'><pre id='5skNy'><center id='5skNy'></center></pre></bdo></b><th id='5skNy'></th></span></q></dt></tr></i><div id='5skNy'><tfoot id='5skNy'></tfoot><dl id='5skNy'><fieldset id='5skNy'></fieldset></dl></div>
    1. <legend id='5skNy'><style id='5skNy'><dir id='5skNy'><q id='5skNy'></q></dir></style></legend>

        python属性查找过程如何工作?

        时间:2023-09-13

          <i id='9Cj4T'><tr id='9Cj4T'><dt id='9Cj4T'><q id='9Cj4T'><span id='9Cj4T'><b id='9Cj4T'><form id='9Cj4T'><ins id='9Cj4T'></ins><ul id='9Cj4T'></ul><sub id='9Cj4T'></sub></form><legend id='9Cj4T'></legend><bdo id='9Cj4T'><pre id='9Cj4T'><center id='9Cj4T'></center></pre></bdo></b><th id='9Cj4T'></th></span></q></dt></tr></i><div id='9Cj4T'><tfoot id='9Cj4T'></tfoot><dl id='9Cj4T'><fieldset id='9Cj4T'></fieldset></dl></div>
          <legend id='9Cj4T'><style id='9Cj4T'><dir id='9Cj4T'><q id='9Cj4T'></q></dir></style></legend>

          • <bdo id='9Cj4T'></bdo><ul id='9Cj4T'></ul>

              <tfoot id='9Cj4T'></tfoot>

              <small id='9Cj4T'></small><noframes id='9Cj4T'>

                    <tbody id='9Cj4T'></tbody>
                  本文介绍了python属性查找过程如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当我说python 属性查找过程"时,我的意思是:当你编写 x.foo 时,python 做了什么??

                  When i say "python attribute lookup proccess" i mean: what python does when you write x.foo??

                  在网上搜索我没有找到太多关于此的文档,我发现的最好的论文之一将过程恢复到以下步骤(你可以看到完整的文章 这里)

                  Searching the web i didn't found to much docs about this, one of the best papers i found resumed the proccess to the following steps (you can see the full article here)

                  1. 如果 attrname 是 objectname 的特殊(即 Python 提供的)属性,则返回它.
                  2. 检查 objectname.__class__.__dict__ 的 attrname.如果存在并且是数据描述符,则返回描述符结果.搜索 objectname.__class__ 的所有基以查找相同的案例.
                  3. 检查 objectname.__dict__ 的 attrname,如果找到则返回.如果 objectname 是一个类,也搜索它的基类.如果它是一个类并且描述符存在于它或其基类中,则返回描述符结果.
                  4. 检查 objectname.__class__.__dict__ 的 attrname.如果存在且是非数据描述符,则返回描述符结果.如果它存在并且不是描述符,则返回它.如果它存在并且是一个数据描述符,我们就不应该在这里,因为我们会在第 2 点返回.搜索 objectname.__class__ 的所有基以查找相同的情况.
                  5. 引发 AttributeError.

                  起初这似乎是正确的,但属性查找过程有点复杂,例如对于 x.foo,如果 x 是类或实例,它的行为就不一样了.

                  At first this might seem right, but the attribute lookup process is a little bit more complicated, for example for x.foo, it doesn't behave the same if x is a class or an instance.

                  我发现了一些无法通过这种方式解释的示例.考虑以下 python 代码:

                  I have a found some samples that can't be explained by this way. Consider the following python code:

                  class Meta(type):
                      def __getattribute__(self, name):
                          print("Metaclass getattribute invoked:", self)
                          return type.__getattribute__(self, name)
                  
                      def __getattr__(self, item):
                          print('Metaclass getattr invoked: ', item)
                          return None
                  
                  class C(object, metaclass=Meta):
                      def __getattribute__(self, name):
                          print("Class getattribute invoked:", args)
                          return object.__getattribute__(self, name)
                  
                  c=C()
                  

                  现在检查以下行与相应的输出:

                  Now check the following lines with the corresponding output:

                  >> C.__new__
                  Metaclass getattribute invoked: <class '__main__.C'>
                  <built-in method __new__ of type object at 0x1E1B80B0>
                  
                  >> C.__getattribute__
                  Metaclass getattribute invoked: <class '__main__.C'>
                  <function __getattribute__ at 0x01457F18>
                  
                  >> C.xyz
                  Metaclass getattribute invoked: <class '__main__.C'>
                  Metaclass getattr invoked:  xyz
                  None
                  
                  >> c.__new__
                  Class getattribute invoked: (<__main__.C object at 0x013E7550>, '__new__')
                  <built-in method __new__ of type object at 0x1E1B80B0>
                  
                  >> c.__getattribute__
                  Class getattribute invoked: (<__main__.C object at 0x01438DB0>, '__getattribute__')
                  Metaclass getattribute invoked: <class '__main__.C'>
                  <bound method C.__getattribute__ of <__main__.C object at 0x01438DB0>>
                  
                  >> 
                  

                  我的结论是(考虑到我们正在搜索 x.foo):

                  The conclusions i have been are (considering we're searching for x.foo):

                  • __getattribute__ 对于 < 的实例是不同的.键入类型"> 和 <键入对象">.对于 C.foo(),首先在 C.__dict__ 上搜索 'foo',如果找到则返回(而不是搜索 type(C)),对于 x.foo(),在 type(x).__dict__ 上搜索 'foo' 和在 x.__dict__ 上.
                  • __getattribute__ 方法总是在 type(x) 上解析,我不明白的是最后一种情况:c.__getattribute__, is not object contains a method __getattribute__ (and C继承 from object),那么为什么元类getattribute 方法被调用.

                  有人可以解释一下吗?或者告诉我在哪里可以找到有关此的文档,谢谢.

                  Can someone explain this please?? or at less tell me where can i find some documentation about this, thanks.

                  推荐答案

                  如果你添加了 print("Metaclass getattribute invoked:", self, name) 你会看到:

                  If you added print("Metaclass getattribute invoked:", self, name) you'd see:

                  >>> c.__getattribute__
                  Class getattribute invoked: <__main__.C object at 0x2acdbb1430d0> __getattribute__
                  Metaclass getattribute invoked: <class '__main__.C'> __name__
                  <bound method C.__getattribute__ of <__main__.C object at 0x2acdbb1430d0>>
                  

                  元类 __getattribute__ 被调用以构建表达式 c.__getattribute__repr,以便它可以打印 C__name__.

                  The metaclass __getattribute__ is getting invoked in order to build the repr of the expression c.__getattribute__, so that it can print C's __name__.

                  btw,__getattribute__ 对类和元类的作用相同;首先在实例上查找属性,然后在实例的类型上查找.

                  btw, __getattribute__ works the same for classes and metaclasses; the attribute is looked up first on the instance then on the instance's type.

                  >>> Meta.foo = 1
                  >>> C.foo
                  ('Metaclass getattribute invoked:', <class '__main__.C'>, 'foo')
                  1
                  >>> c.foo
                  ('Class getattribute invoked:', <__main__.C object at 0x2acdbb1430d0>, 'foo')
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "<stdin>", line 5, in __getattribute__
                  AttributeError: 'C' object has no attribute 'foo'
                  >>> C.bar = 2
                  >>> c.bar
                  ('Class getattribute invoked:', <__main__.C object at 0x2acdbb1430d0>, 'bar')
                  2
                  

                  这篇关于python属性查找过程如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么当我尝试访问班级中的属性时会收到 Nam 下一篇:装饰器设置函数的属性

                  相关文章

                  最新文章

                  <tfoot id='9ZRla'></tfoot>
                  <legend id='9ZRla'><style id='9ZRla'><dir id='9ZRla'><q id='9ZRla'></q></dir></style></legend>

                    <i id='9ZRla'><tr id='9ZRla'><dt id='9ZRla'><q id='9ZRla'><span id='9ZRla'><b id='9ZRla'><form id='9ZRla'><ins id='9ZRla'></ins><ul id='9ZRla'></ul><sub id='9ZRla'></sub></form><legend id='9ZRla'></legend><bdo id='9ZRla'><pre id='9ZRla'><center id='9ZRla'></center></pre></bdo></b><th id='9ZRla'></th></span></q></dt></tr></i><div id='9ZRla'><tfoot id='9ZRla'></tfoot><dl id='9ZRla'><fieldset id='9ZRla'></fieldset></dl></div>
                      <bdo id='9ZRla'></bdo><ul id='9ZRla'></ul>

                    1. <small id='9ZRla'></small><noframes id='9ZRla'>