Python __getattribute__ vs __getattr__
# 例子在原来的基础上简化了一下,排除依赖和干扰,详细参见原项目
class UrlGenerator(object):
def __init__(self, root_url):
self.url = root_url def __getattr__(self, item):
if item == 'get' or item == 'post':
print self.url
return UrlGenerator('{}/{}'.format(self.url, item)) url_gen = UrlGenerator('http://xxxx')
url_gen.users.show.get >>> http://xxxx/users/show
充分利用getattr会在没有查找到相应实例属性时被调用的特点,方便的通过链式调用生成对应的url,源代码中在碰到http method的时候返回一个
可调用的对象更加的优雅,链式的操作不仅优雅而且还能很好的说明调用的接口的意义(restful的接口啦)。
既然能通过定制类的getattr自定义方法来实现一些优雅的功能,自然我们也要对它有一些了解,包括和它相似的自定义方法getattribute
1. 用作实例属性的获取和拦截
当访问某个实例属性时, getattribute会被无条件调用,如未实现自己的getattr方法,会抛出AttributeError提示找不到这个属性,如果自定义了自己getattr方法的话,方法会在这种找不到属性的情况下被调用,比如上面的例子中的情况。所以在找不到属性的情况下通过实现自定义的getattr方法来实现一些功能是一个不错的方式,因为它不会像getattribute方法每次都会调用可能会影响一些正常情况下的属性访问:
class Test(object):
def __init__(self, p):
self.p = p def __getattr__(self, item):
return 'default' t = Test('p1')
print t.p
print t.p2 >>> p1
>>> default
2. 自定义getattribute的时候防止无限递归
因为getattribute在访问属性的时候一直会被调用,自定义的getattribute方法里面同时需要返回相应的属性,通过self.__dict__取值会继续向下调用getattribute,造成循环调用:
class AboutAttr(object):
def __init__(self, name):
self.name = name def __getattribute__(self, item):
try:
return super(AboutAttr, self).__getattribute__(item)
except KeyError:
return 'default'
这里通过调用绑定的super对象来获取队形的属性,对新式类来说其实和object.__getattribute__(self, item)一样的道理:
- 默认情况下自定义的类会从object继承
getattribute方法,对于属性的查找是完全能用的 - getattribute的实现感觉还是挺抽象化的,只需要绑定相应的实例对象和要查找的属性名称就行
3.同时覆盖掉getattribute和getattr的时候,在getattribute中需要模仿原本的行为抛出AttributeError或者手动调用getattr
class AboutAttr(object):
def __init__(self, name):
self.name = name def __getattribute__(self, item):
try:
return super(AboutAttr, self).__getattribute__(item)
except KeyError:
return 'default'
except AttributeError as ex:
print ex def __getattr__(self, item):
return 'default' at = AboutAttr('test')
print at.name
print at.not_exised >>>test
>>>'AboutAttr' object has no attribute 'not_exised'
>>>None
上面例子里面的getattr方法根本不会被调用,因为原本的AttributeError被我们自行处理并未抛出,也没有手动调用getattr,所以访问not_existed的结果是None而不是default.
Python __getattribute__ vs __getattr__的更多相关文章
- python __getattribute__、__getattr__、__setattr__详解
__getattribute__ 官方文档中描述如下: 该方法可以拦截对对象属性的所有访问企图,当属性被访问时,自动调用该方法(只适用于新式类).因此常用于实现一些访问某属性时执行一段代码的特性. 需 ...
- python中的__getattr__、__getattribute__、__setattr__、__delattr__、__dir__
__getattr__: 属性查找失败后,解释器会调用 __getattr__ 方法. class TmpTest: def __init__(self): self.tmp = 'tmp12 ...
- day26 Python __getattribute__
__getattr__#不存在的属性访问,触发__getattr__ class Foo: def __init__(self,x): self.x=x def __getattr__(self, i ...
- python 中__setattr__, __getattr__,__getattribute__, __call__使用方法
object._getattr_(self, name) 拦截点号运算.当对未定义的属性名称和实例进行点号运算时,就会用属性名作为字符串调用这个方法.如果继承树可以找到该属性,则不调用此方法 实例in ...
- Python中__get__, __getattr__, __getattribute__的区别及延迟初始化
本节知识点 1.__get__, __getattr__, __getattribute__的区别 2.__getattr__巧妙应用 3.延迟初始化(lazy property) 1.__get__ ...
- Python魔法方法__getattr__和__getattribute__详解
在Python中有这两个魔法方法容易让人混淆:__getattr__和getattribute.通常我们会定义__getattr__而从来不会定义getattribute,下面我们来看看这两个的区别. ...
- python __setattr__、__getattr__、__getattribute__全面详解
一.属性引用函数 hasattr(obj,name[,default])getattr(obj,name)setattr(obj,name,value)delattr(obj,name) 二.属性引用 ...
- python中__get__,__getattr__,__getattribute__的区别
__get__,__getattr__和__getattribute都是访问属性的方法,但不太相同. object.__getattr__(self, name) 当一般位置找不到attribute的 ...
- Python的__getattribute__ vs __getattr__的妙用
这里的属性即包括属性变量,也包括属性方法.即类的变量和方法. 当访问某个实例属性时, getattribute会被无条件调用,如未实现自己的getattr方法,会抛出AttributeError提示找 ...
随机推荐
- Android 用户界面---定制组件(Custom Components)
基于布局类View和ViewGroup的基本功能,Android为创建自己的UI界面提供了先进和强大的定制化模式.首先,平台包含了各种预置的View和ViewGroup子类---Widget和layo ...
- L143 Seasonal 'Plague' Hits College Freshman
Sometimes, all the hand sanitizer in the world cannot prevent the inevitable.College freshmen across ...
- Wordpress在主题或者插件中自定义存储附件的方法
1.前端使用form表单 //单文件上传,我的业务需求中限制了必须上传图片 <input type="file" name="singlename" ac ...
- 没有绝对的cc.ResolutionPolicy.FIXED_WIDTH或cc.ResolutionPolicy.FIXED_HEIGHT
以做cocos手游的经验来说,为了保证游戏在各种尺寸屏幕完美展现,没有黑边,没有非等比缩放,所以基本上适配机制都是都是cc.ResolutionPolicy.FIXED_WIDTH或cc.Resolu ...
- hibernate的级联(hibernate注解的CascadeType属性)
[自己项目遇到的问题]: 新增 删除都可以实现 ,就是修改的时候无法同步更新设计三个类: 问题类scask 正文内容类text类 查看数+回复数+讨论数的runinfo类 [正文类和查看数 ...
- asp.net core mcroservices 架构之 分布式日志(三):集成kafka
一 kafka介绍 kafka是基于zookeeper的一个分布式流平台,既然是流,那么大家都能猜到它的存储结构基本上就是线性的了.硬盘大家都知道读写非常的慢,那是因为在随机情况下,线性下,硬盘的读写 ...
- 剑指offer-第三章高质量代码(树的子结构)
题目:输入两个二叉树A和B,判断B是不是A的子结构. 思路:遍历A树找到B树的根节点,然后再判断左右子树是否相同.不相同再往下找.重复改过程. 子结构的描述如下图所示: C++代码: #include ...
- 阿里云接口异常-Can not find endpoint to access
最近在做公司的资产盘点,需要请求阿里云的接口获取公司的云服务器信息.在获取实例列表的过程中,通过异常机制捕获了 Can not find endpoint to access 这个错误.经过多次排查, ...
- ②HttpURLConnection通过Json参数方式提交Post请求
之前的文章介绍过通过报文的方式HttpURLConnection提交post请求,今天介绍下通过Json参数的方法提交Post请求,先上代码 public static HttpResponse se ...
- [转]你真的了解setTimeout和setInterval吗?
原文: http://qingbob.com/difference-between-settimeout-setinterval/ setTimeout和setInterval的基本用法我们一带而过: ...