python2/python3中__get__、 __getattr__、 __getatrribute__的区别

python2的类分为经典类和新式类,派生自object的类都是新式类,python3的类都是新式类,默认派生自object。python2的__get__、 __getattr__、 __getatrribute__函数和python3的略有不同,下面分点说明。

1.__get__(self, instance, owner)

__get__(self, instance, owner)函数只有在新式类中有意义,在经典类中无意义。在新式的class中,如果定义了__get__,则这个类可以成为descriptor。owner为把descriptor设为类属性的class, instance为该class的实例。如果通过class访问descriptor,则instance为None。
测试代码如下图所示,左中右分别为python2经典类、python2新式类、python3的类:


有图可以看出,__get__在python2经典类中无意义,在python2新式类中和python3的意义一样。

2.__getattr__(self,  attr)、 __getatrribute__(self, attr)

(1)经典类中的__getattr__,__getatrribute__

__getatrribute__函数在经典类中无意义,__getattr__函数在经典类中有意义。测试代码如下。
>>> class A:
	def __init__(self, x):
		self.x = x
	def __getattr__(self, attr):
		return 'getattr: ', attr

	
>>> class B:
	def __init__(self, x):
		self.x = x
	def __getattribute__(self, attr):
		return 'getattribute: ', attr

	
>>> a = A('foo'); b = B('bar')
>>> 
>>> a.x
'foo'
>>> b.x
'bar'
>>> a.y
('getattr: ', 'y')
>>> b.y

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    b.y
AttributeError: B instance has no attribute 'y'
>>> 

(2)新式类中的__getattr__,__getatrribute__

同时定义了__getattr__和__getattribute__的话,访问类实例属性只会调用__getattribute__,不管该属性是否存在。只定义了__getattr__没有定义__getattribute__的情况下,访问存在的类实例属性不会调用__getattr__,会返回该属性的值;访问不存在的实例属性的话会调用__getattr__。测试代码见下图,左边为python2新式类,右边为python3.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值