您的位置:首页 > 编程语言 > Python开发

Python语言学习讲解十七:python之__gettattr__、__getattribute__和__setattr__详解

2016-12-01 15:49 591 查看
注:每周一到周五都会进行相关Python基础知识更新,欢迎大家提宝贵的意见

python语言提供了一种挂钩,使得开发者能够很方便的编写出通用代码,他们使用的不是普通实例的属性,@property方法和描述符,而是使用的是python的魔术方法
__gettattr__、__getattribute__和__setattr__,他们属于动态行为。

>>> class TEST(object):

def __init__(self):
self.value = 1
def __getattr__(self, name):
value = 'Value for %s' % name
setattr(self, name, value)
return value

>>> testobj = TEST()

>>> print('---:',testobj.__dict__)
{'__methods__': 'Value for __methods__', '__members__': 'Value for __members__', 'value': 1}

>>> print(testobj.__dict__)
{'__methods__': 'Value for __methods__', '__members__': 'Value for __members__', 'value': 1}

>>> print(testobj.attr)
'Value for attr'

>>> print(testobj.__dict__)
{'__methods__': 'Value for __methods__', '__members__': 'Value for __members__', 'value': 1, 'attr': 'Value for attr'}

总结:

__gettattr__:如果某个类定义了这个方法,并且在该类的对象的字典中又找不到相应的属性时候,那么次方法会被调用。

__getattribute__:不管对象的字典中有没有找到对应的属性,都会调用


__setattr__:无论是直接赋值还是通过内置的setattr函数赋值,都会调用

********还有一点需要住的是__[b]getattribute__和__setattr__方法中访问实例属性的时候,应该直接通过super()来做,避免无线递归。******[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息