蟒蛇属性查找过程是如何工作的? [英] How python attribute lookup process works?

查看:160
本文介绍了蟒蛇属性查找过程是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我说蟒蛇属性查找proccess我的意思是:什么Python做当你写x.foo ??

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

搜索网站,我没有发现这个文档太多,我找到了最佳论文之一恢复了proccess以下步骤(你可以看到完整的文章<一个href=\"http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html\">here)

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是对象名特殊(即Python的提供)的属性,将其返回。

  2. 检查对象名.__类__.__ dict__为attrname。如果它存在,是一个数据描述符,返回描述的结果。搜索对象名class__的.__所有基地同一案件。

  3. 检查对象名.__ dict__为attrname,并返回如果找到。如果objectname是一个类,搜索它的基地了。如果它是一个类和描述符它或它的基地存在,返回结果描述符。

  4. 检查对象名.__类__.__ dict__为attrname。如果存在,是一个非数据描述符,返回描述符的结果。如果它存在,而不是一个描述符,只是返回。如果它存在,是一个数据描述符,我们不应该在这里,因为我们将在点2搜索已返回对象名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.

我有一个发现有些样品不能由这种方式进行说明。请看下面的蟒蛇code:

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__是与LT的情况不同;类型'类型'>和&lt;键入对象>。对于C.foo(),'富'首先搜索基于C .__ dict__并返回若发现(而不是搜索类型(C))和x.foo()'富'被搜索的类型(X).__ dict__和在X .__字典__。

  • __ getattribute__方法总是解决的类型(X),我不明白这里是最后一种情况:C .__ getattribute__,不是对象包含一个方法__getattribute__(和C从对象继承),那么,为什么元类getAttribute方法被调用。

有人能解释一下?请或更少的告诉我,我在哪里可以找到有关这一些文档,谢谢。

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

推荐答案

如果您添加打印(元类的getAttribute调用:自我,姓名)你会看到

>>> 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>>

元类 __的ge​​tAttribute __ 是越来越调用,以构建再版除权pression <$的C $ C>ç.__的ge​​tAttribute __ ,以便它可以打印 C __名__ >

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__.

顺便说一句, __ __的ge​​tAttribute 工程类和元类相同;该属性抬头首先在该实例然后在该实例的类型。

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆