我试图理解什么时候定义__getattr__或__getattribute__。python文档提到__getattribute__适用于新样式的类。什么是新型类?


当前回答

在阅读Beazley & Jones PCB时,我偶然发现了__getattr__的一个明确而实际的用例,它有助于回答OP问题的“何时”部分。摘自书中:

"The __getattr__() method is kind of like a catch-all for attribute lookup. It's a method that gets called if code tries to access an attribute that doesn't exist." We know this from the above answers, but in PCB recipe 8.15, this functionality is used to implement the delegation design pattern. If Object A has an attribute Object B that implements many methods that Object A wants to delegate to, rather than redefining all of Object B's methods in Object A just to call Object B's methods, define a __getattr__() method as follows:

def __getattr__(self, name):
    return getattr(self._b, name)

其中_b是Object A的属性(Object B)的名称。当Object B上定义的方法在Object A上被调用时,__getattr__方法将在查找链的末尾被调用。这也会使代码更干净,因为您不需要为委托给另一个对象而定义一个方法列表。

其他回答

__getattr__和__getattribute__之间的一个关键区别是,__getattr__只在没有以通常的方式找到属性时才会被调用。它很适合为缺失的属性实现回退,并且可能是您想要的两种方法之一。

__getattribute__是在查看对象上的实际属性之前调用的,因此正确实现可能很棘手。你可以很容易地得到无限递归。

新类型的类派生于object,旧类型的类是Python 2中的类。没有显式基类的X。但是在__getattr__和__getattribute__之间进行选择时,旧风格和新风格类之间的区别并不是重要的一个。

你几乎肯定想要__getattr__。

新型类是直接或间接继承“object”的类。除了__init__之外,它们还有一个__new__类方法,并且有一些更合理的低级行为。

通常,你会想要重写__getattr__(如果你重写了其中任何一个),否则你将很难支持“self”。方法中的Foo语法。

额外信息:http://www.devx.com/opensource/Article/31482/0/page/4

这只是一个基于Ned Batchelder解释的例子。

__getattr__例子:

class Foo(object):
    def __getattr__(self, attr):
        print "looking up", attr
        value = 42
        self.__dict__[attr] = value
        return value

f = Foo()
print f.x 
#output >>> looking up x 42

f.x = 3
print f.x 
#output >>> 3

print ('__getattr__ sets a default value if undefeined OR __getattr__ to define how to handle attributes that are not found')

如果同样的例子使用__getattribute__,你会得到>>> RuntimeError:调用Python对象时超过最大递归深度

在阅读Beazley & Jones PCB时,我偶然发现了__getattr__的一个明确而实际的用例,它有助于回答OP问题的“何时”部分。摘自书中:

"The __getattr__() method is kind of like a catch-all for attribute lookup. It's a method that gets called if code tries to access an attribute that doesn't exist." We know this from the above answers, but in PCB recipe 8.15, this functionality is used to implement the delegation design pattern. If Object A has an attribute Object B that implements many methods that Object A wants to delegate to, rather than redefining all of Object B's methods in Object A just to call Object B's methods, define a __getattr__() method as follows:

def __getattr__(self, name):
    return getattr(self._b, name)

其中_b是Object A的属性(Object B)的名称。当Object B上定义的方法在Object A上被调用时,__getattr__方法将在查找链的末尾被调用。这也会使代码更干净,因为您不需要为委托给另一个对象而定义一个方法列表。

我发现没有人提到这个区别:

__getattribute__有默认实现,但__getattr__没有。

class A:
    pass
a = A()
a.__getattr__ # error
a.__getattribute__ # return a method-wrapper

这有一个明确的含义:因为__getattribute__有默认实现,而__getattr__没有,显然python鼓励用户实现__getattr__。