问题:了解__getattr__和__getattribute__之间的区别
我试图理解上的差异之间__getattr__
和__getattribute__
,但是,我在它失败。
堆栈溢出问题的答案与vs 之间的区别是__getattr__
__getattribute__
:
__getattribute__
在查看对象的实际属性之前调用,因此很难正确实现。您可以非常轻松地进行无限递归。
我完全不知道那是什么意思。
然后继续说:
您几乎可以肯定想要__getattr__
。
为什么?
我读到,如果__getattribute__
失败,__getattr__
则称为。那么,为什么有两种不同的方法做同样的事情呢?如果我的代码实现了新样式类,我应该使用什么?
我正在寻找一些代码示例来清除此问题。我已尽我所能搜索Google,但是我发现的答案并未彻底讨论该问题。
如果有任何文档,我准备阅读。
I am trying to understand the difference between __getattr__
and __getattribute__
, however, I am failing at it.
The answer to the Stack Overflow question Difference between __getattr__
vs __getattribute__
says:
__getattribute__
is invoked before looking at the actual attributes on
the object, and so can be tricky to
implement correctly. You can end up in
infinite recursions very easily.
I have absolutely no idea what that means.
Then it goes on to say:
You almost certainly want __getattr__
.
Why?
I read that if __getattribute__
fails, __getattr__
is called. So why are there two different methods doing the same thing? If my code implements the new style classes, what should I use?
I am looking for some code examples to clear this question. I have Googled to best of my ability, but the answers that I found don’t discuss the problem thoroughly.
If there is any documentation, I am ready to read that.
回答 0
首先了解一些基础知识。
对于对象,您需要处理其属性。通常我们会这么做instance.attribute
。有时我们需要更多的控制权(当我们事先不知道属性名称时)。
例如,instance.attribute
将变为getattr(instance, attribute_name)
。使用此模型,我们可以通过提供attribute_name作为字符串来获取属性。
用于 __getattr__
您还可以告诉类如何处理它未显式管理的属性,并通过__getattr__
方法进行操作。
每当您请求尚未定义的属性时,Python都会调用此方法,因此您可以定义该方法。
一个经典的用例:
class A(dict):
def __getattr__(self, name):
return self[name]
a = A()
# Now a.somekey will give a['somekey']
注意事项和使用 __getattribute__
如果您需要捕获每个属性(无论是否存在),请使用__getattribute__
。不同之处在于,__getattr__
仅调用实际上不存在的属性。如果您直接设置属性,则引用该属性将无需调用即可检索它__getattr__
。
__getattribute__
一直被称为。
Some basics first.
With objects, you need to deal with its attributes. Ordinarily we do instance.attribute
. Sometimes we need more control (when we do not know the name of the attribute in advance).
For example, instance.attribute
would become getattr(instance, attribute_name)
. Using this model, we can get the attribute by supplying the attribute_name as a string.
Use of __getattr__
You can also tell a class how to deal with attributes which it doesn’t explicitly manage and do that via __getattr__
method.
Python will call this method whenever you request an attribute that hasn’t already been defined, so you can define what to do with it.
A classic use case:
class A(dict):
def __getattr__(self, name):
return self[name]
a = A()
# Now a.somekey will give a['somekey']
Caveats and use of __getattribute__
If you need to catch every attribute regardless whether it exists or not, use __getattribute__
instead. The difference is that __getattr__
only gets called for attributes that don’t actually exist. If you set an attribute directly, referencing that attribute will retrieve it without calling __getattr__
.
__getattribute__
is called all the times.
回答 1
__getattribute__
每当发生属性访问时都会调用。
class Foo(object):
def __init__(self, a):
self.a = 1
def __getattribute__(self, attr):
try:
return self.__dict__[attr]
except KeyError:
return 'default'
f = Foo(1)
f.a
这将导致无限递归。罪魁祸首是排队return self.__dict__[attr]
。让我们假装(这与事实很接近)所有属性都存储在self.__dict__
名称中并可用。线
f.a
尝试访问的a
属性f
。这叫f.__getattribute__('a')
。__getattribute__
然后尝试加载self.__dict__
。__dict__
是的属性,self == f
因此python调用f.__getattribute__('__dict__')
再次尝试访问属性'__dict__
‘。这是无限递归。
如果__getattr__
曾经使用过,那么
- 它永远不会运行,因为
f
具有a
属性。
- 如果它已经运行((假设您要
f.b
)),则不会调用__dict__
它,因为它已经存在,并且__getattr__
仅当所有其他查找属性的方法均失败时才被调用。
编写上述类的“正确”方法__getattribute__
是
class Foo(object):
# Same __init__
def __getattribute__(self, attr):
return super(Foo, self).__getattribute__(attr)
super(Foo, self).__getattribute__(attr)
将__getattribute__
“最近”超类的方法(self
通常是该类的“方法解析顺序”中的下一个类)绑定到当前对象,然后调用它并让其完成工作。
通过使用__getattr__
python 可以在没有找到属性之前将其正常处理,从而避免了所有这些麻烦。到那时,Python将控制权交给您的__getattr__
方法,并让它提出一些建议。
还值得注意的是,您可以使用进行无限递归__getattr__
。
class Foo(object):
def __getattr__(self, attr):
return self.attr
我将把它留作练习。
__getattribute__
is called whenever an attribute access occurs.
class Foo(object):
def __init__(self, a):
self.a = 1
def __getattribute__(self, attr):
try:
return self.__dict__[attr]
except KeyError:
return 'default'
f = Foo(1)
f.a
This will cause infinite recursion. The culprit here is the line return self.__dict__[attr]
. Let’s pretend (It’s close enough to the truth) that all attributes are stored in self.__dict__
and available by their name. The line
f.a
attempts to access the a
attribute of f
. This calls f.__getattribute__('a')
. __getattribute__
then tries to load self.__dict__
. __dict__
is an attribute of self == f
and so python calls f.__getattribute__('__dict__')
which again tries to access the attribute '__dict__
‘. This is infinite recursion.
If __getattr__
had been used instead then
- It never would have run because
f
has an a
attribute.
- If it had run, (let’s say that you asked for
f.b
) then it would not have been called to find __dict__
because it’s already there and __getattr__
is invoked only if all other methods of finding the attribute have failed.
The ‘correct’ way to write the above class using __getattribute__
is
class Foo(object):
# Same __init__
def __getattribute__(self, attr):
return super(Foo, self).__getattribute__(attr)
super(Foo, self).__getattribute__(attr)
binds the __getattribute__
method of the ‘nearest’ superclass (formally, the next class in the class’s Method Resolution Order, or MRO) to the current object self
and then calls it and lets that do the work.
All of this trouble is avoided by using __getattr__
which lets Python do it’s normal thing until an attribute isn’t found. At that point, Python hands control over to your __getattr__
method and lets it come up with something.
It’s also worth noting that you can run into infinite recursion with __getattr__
.
class Foo(object):
def __getattr__(self, attr):
return self.attr
I’ll leave that one as an exercise.
回答 2
我认为其他的答案做了解释之间的差异的一个伟大的工作__getattr__
和__getattribute__
,但有一点可能没有明确的是,为什么你会想使用__getattribute__
。有趣的__getattribute__
是,它本质上允许您在访问类时重载点。这使您可以自定义如何在较低级别访问属性。例如,假设我要定义一个类,其中所有仅带有自变量的方法都被视为属性:
# prop.py
import inspect
class PropClass(object):
def __getattribute__(self, attr):
val = super(PropClass, self).__getattribute__(attr)
if callable(val):
argcount = len(inspect.getargspec(val).args)
# Account for self
if argcount == 1:
return val()
else:
return val
else:
return val
从交互式解释器中:
>>> import prop
>>> class A(prop.PropClass):
... def f(self):
... return 1
...
>>> a = A()
>>> a.f
1
当然,这是一个愚蠢的示例,您可能永远也不想这样做,但是它向您展示了从覆盖获得的强大功能__getattribute__
。
I think the other answers have done a great job of explaining the difference between __getattr__
and __getattribute__
, but one thing that might not be clear is why you would want to use __getattribute__
. The cool thing about __getattribute__
is that it essentially allows you to overload the dot when accessing a class. This allows you to customize how attributes are accessed at a low level. For instance, suppose I want to define a class where all methods that only take a self argument are treated as properties:
# prop.py
import inspect
class PropClass(object):
def __getattribute__(self, attr):
val = super(PropClass, self).__getattribute__(attr)
if callable(val):
argcount = len(inspect.getargspec(val).args)
# Account for self
if argcount == 1:
return val()
else:
return val
else:
return val
And from the interactive interpreter:
>>> import prop
>>> class A(prop.PropClass):
... def f(self):
... return 1
...
>>> a = A()
>>> a.f
1
Of course this is a silly example and you probably wouldn’t ever want to do this, but it shows you the power you can get from overriding __getattribute__
.
回答 3
我经历了别人的出色解释。但是,我从此博客Python Magic Methods和__getattr__
找到了一个简单的答案。以下所有都是从那里开始的。
使用__getattr__
magic方法,我们可以拦截不存在的属性查找并做一些事情,以确保它不会失败:
class Dummy(object):
def __getattr__(self, attr):
return attr.upper()
d = Dummy()
d.does_not_exist # 'DOES_NOT_EXIST'
d.what_about_this_one # 'WHAT_ABOUT_THIS_ONE'
但是,如果该属性确实存在,__getattr__
将不会被调用:
class Dummy(object):
def __getattr__(self, attr):
return attr.upper()
d = Dummy()
d.value = "Python"
print(d.value) # "Python"
__getattribute__
与相似__getattr__
,但重要的区别是__getattribute__
将拦截每个属性查找,而属性是否存在无关紧要。
class Dummy(object):
def __getattribute__(self, attr):
return 'YOU SEE ME?'
d = Dummy()
d.value = "Python"
print(d.value) # "YOU SEE ME?"
在该示例中,d
对象已经具有属性值。但是,当我们尝试访问它时,没有得到原始的期望值(“ Python”);我们只是得到任何__getattribute__
回报。这意味着我们实际上失去了value属性;它已经变得“无法到达”。
I have gone through other’s excellent explanation. However, I found a simple answer from this blog Python Magic Methods and __getattr__
. All the following are from there.
Using the __getattr__
magic method, we can intercept that inexistent attribute lookup and do something so it doesn’t fail:
class Dummy(object):
def __getattr__(self, attr):
return attr.upper()
d = Dummy()
d.does_not_exist # 'DOES_NOT_EXIST'
d.what_about_this_one # 'WHAT_ABOUT_THIS_ONE'
But if the attribute does exist, __getattr__
won’t be invoked:
class Dummy(object):
def __getattr__(self, attr):
return attr.upper()
d = Dummy()
d.value = "Python"
print(d.value) # "Python"
__getattribute__
is similar to __getattr__
, with the important difference that __getattribute__
will intercept EVERY attribute lookup, doesn’t matter if the attribute exists or not.
class Dummy(object):
def __getattribute__(self, attr):
return 'YOU SEE ME?'
d = Dummy()
d.value = "Python"
print(d.value) # "YOU SEE ME?"
In that example, the d
object already has an attribute value. But when we try to access it, we don’t get the original expected value (“Python”); we’re just getting whatever __getattribute__
returned. It means that we’ve virtually lost the value attribute; it has become “unreachable”.