我试图理解什么时候定义__getattr__或__getattribute__。python文档提到__getattribute__适用于新样式的类。什么是新型类?
当前回答
我发现没有人提到这个区别:
__getattribute__有默认实现,但__getattr__没有。
class A:
pass
a = A()
a.__getattr__ # error
a.__getattribute__ # return a method-wrapper
这有一个明确的含义:因为__getattribute__有默认实现,而__getattr__没有,显然python鼓励用户实现__getattr__。
其他回答
__getattr__和__getattribute__之间的一个关键区别是,__getattr__只在没有以通常的方式找到属性时才会被调用。它很适合为缺失的属性实现回退,并且可能是您想要的两种方法之一。
__getattribute__是在查看对象上的实际属性之前调用的,因此正确实现可能很棘手。你可以很容易地得到无限递归。
新类型的类派生于object,旧类型的类是Python 2中的类。没有显式基类的X。但是在__getattr__和__getattribute__之间进行选择时,旧风格和新风格类之间的区别并不是重要的一个。
你几乎肯定想要__getattr__。
getattribute:用于从实例中检索属性。它通过使用点表示法或getattr()内置函数捕获访问实例属性的每次尝试。 getattr:当在对象中找不到属性时,作为最后一个资源执行。您可以选择返回一个默认值或引发AttributeError。
返回__getattribute__函数;如果没有覆盖默认实现;执行该方法时执行以下检查:
检查MRO链中的任何类中是否定义了同名(属性名)的描述符(方法对象解析) 然后查看实例的名称空间 然后查看类名称空间 然后进入每个基的名称空间,等等。 最后,如果未找到,默认实现将调用实例的回退getattr()方法,并作为默认实现引发AttributeError异常。
这是对象的实际实现。__getattribute__方法:
.. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name) Generic attribute getter function that is meant to be put into a type object's tp_getattro slot. It looks for a descriptor in the dictionary of classes in the object's MRO as well as an attribute in the object's :attr:~object.dict (if present). As outlined in :ref:descriptors, data descriptors take preference over instance attributes, while non-data descriptors don't. Otherwise, an :exc:AttributeError is raised.
我发现没有人提到这个区别:
__getattribute__有默认实现,但__getattr__没有。
class A:
pass
a = A()
a.__getattr__ # error
a.__getattribute__ # return a method-wrapper
这有一个明确的含义:因为__getattribute__有默认实现,而__getattr__没有,显然python鼓励用户实现__getattr__。
new -style类继承自object或另一个new -style类:
class SomeObject(object):
pass
class SubObject(SomeObject):
pass
老式的类没有:
class SomeObject:
pass
这只适用于python2 -在python3中,上述所有将创建新样式的类。
看到9。类(Python教程),NewClassVsClassicClass和Python中新旧风格类之间的区别是什么?获取详细信息。
让我们看一些__getattr__和__getattribute__神奇方法的简单例子。
__getattr__
每当你请求一个尚未定义的属性时,Python将调用__getattr__方法。在下面的例子中,我的类Count没有__getattr__方法。在main中,当我访问两个obj1时。Mymin和obj1。Mymax属性一切正常。但当我访问obj1时。` Count `对象没有属性` mycurrent `
class Count():
def __init__(self,mymin,mymax):
self.mymin=mymin
self.mymax=mymax
obj1 = Count(1,10)
print(obj1.mymin)
print(obj1.mymax)
print(obj1.mycurrent) --> AttributeError: 'Count' object has no attribute 'mycurrent'
现在我的类Count有__getattr__方法。当我访问obj1时。mycurrent属性——python返回我在__getattr__方法中实现的任何内容。在我的例子中,每当我试图调用一个不存在的属性时,python都会创建该属性并将其设置为整数值0。
class Count:
def __init__(self,mymin,mymax):
self.mymin=mymin
self.mymax=mymax
def __getattr__(self, item):
self.__dict__[item]=0
return 0
obj1 = Count(1,10)
print(obj1.mymin)
print(obj1.mymax)
print(obj1.mycurrent1)
__getattribute__
现在让我们看看__getattribute__方法。如果你的类中有__getattribute__方法,python会为每个属性调用这个方法,不管它是否存在。那么为什么我们需要__getattribute__方法呢?一个很好的理由是,您可以阻止对属性的访问,并使它们更加安全,如下面的示例所示。
每当有人试图访问以子字符串“cur”开头的属性时,python会引发AttributeError异常。否则它会返回该属性。
class Count:
def __init__(self,mymin,mymax):
self.mymin=mymin
self.mymax=mymax
self.current=None
def __getattribute__(self, item):
if item.startswith('cur'):
raise AttributeError
return object.__getattribute__(self,item)
# or you can use ---return super().__getattribute__(item)
obj1 = Count(1,10)
print(obj1.mymin)
print(obj1.mymax)
print(obj1.current)
重要提示:为了避免__getattribute__方法中的无限递归,它的实现应该始终调用具有相同名称的基类方法来访问所需的任何属性。例如:object。__getattribute__(self, name)或super().__getattribute__(item)而不是self.__dict__[item]
重要的
如果你的类同时包含getattr和getattribute魔法方法,那么__getattribute__将首先被调用。但是如果__getattribute__引发 AttributeError异常,则该异常将被忽略,并调用__getattr__方法。示例如下:
class Count(object):
def __init__(self,mymin,mymax):
self.mymin=mymin
self.mymax=mymax
self.current=None
def __getattr__(self, item):
self.__dict__[item]=0
return 0
def __getattribute__(self, item):
if item.startswith('cur'):
raise AttributeError
return object.__getattribute__(self,item)
# or you can use ---return super().__getattribute__(item)
# note this class subclass object
obj1 = Count(1,10)
print(obj1.mymin)
print(obj1.mymax)
print(obj1.current)
推荐文章
- ValueError: numpy。Ndarray大小改变,可能表示二进制不兼容。期望从C头得到88,从PyObject得到80
- Anaconda /conda -安装特定的软件包版本
- 我在哪里调用Keras的BatchNormalization函数?
- 打印测试执行时间并使用py.test锁定缓慢的测试
- 插入一行到熊猫数据框架
- 要列出Pandas DataFrame列
- 在Django模型中存储电话号码的最佳方法是什么?
- 从导入的模块中模拟函数
- 滚动或滑动窗口迭代器?
- python的方法找到最大值和它的索引在一个列表?
- 如何读取文件的前N行?
- 如何删除matplotlib中的顶部和右侧轴?
- 解析.py文件,读取AST,修改它,然后写回修改后的源代码
- Visual Studio Code:如何调试Python脚本的参数
- 使用元组/列表等等。从输入vs直接引用类型如list/tuple/etc