传送门

  1. https://docs.python.org/3/reference/datamodel.html#object.__getattr__
  2. https://docs.python.org/3/reference/datamodel.html#object.__getattribute__
  3. https://stackoverflow.com/questions/4295678/understanding-the-difference-between-getattr-and-getattribute
  4. https://stackoverflow.com/questions/3278077/difference-between-getattr-vs-getattribute

区别

  1. 当正常访问属性失败,即f.x,x不存在,会触发__getattr__
  2. 无论有无f.x,都会触发__getattibute__

Talk is cheap

__getattr__单个出现

class Foo:

    def __init__(self, x):
self.x = x def __getattr__(self, item):
#return self.__dict__[item]
return '__getattr__' f1 = Foo(1)
print(f1.x)
print(f1.xxxxxx) #访问不存在的属性访问,触发__getattr__

__getattribute__单个出现

class Foo:

    def __init__(self, x):
self.x = x def __getattribute__(self, item):
return '__getattribute__' f1 = Foo(1)
print(f1.x) # 访问存在的属性,也会直接执行__getattribute__
print(f1.xxx) # 访问不存在的属性,也会直接执行__getattribute__

__getattr__和__getattibute__同时存在

class Foo:

    def __init__(self, x):
self.x = x def __getattr__(self, item):
return '执行的是我,__getattr__'
# return self.__dict__[item] def __getattribute__(self, item):
print('不管属性是否存在,我都执行,__getattribute__')
raise AttributeError('__getattribute__的AttributeError') f1 = Foo(1)
# 当两者同时存在的时候,只会执行__getattribute__;如果在__getattribute__过程中抛出AttributeError,那么就执行__geattr__
print(f1.x)
# print(f1.xx)

18+ Warning

最好不要使用__getattribute__,不然有可能导致无限递归的可能。

Python - __getattr__和__getattribute__的区别的更多相关文章

  1. 浅谈Python 中 __getattr__与__getattribute__的区别

    __getattr__与__getattribute__均是一般实例属性截取函数(generic instance attribute interception method),其中,__getatt ...

  2. python __getattr__和 __getattribute__

    __getattr__ 这个魔法函数会在类中查找不到属性时调用 class User: def __init__(self): self.info = 1 def __getattr__(self, ...

  3. __getattr__ 与 __getattribute__的区别

    原文博客地址 http://www.cnblogs.com/bettermanlu/archive/2011/06/22/2087642.html

  4. Python中__get__, __getattr__, __getattribute__的区别及延迟初始化

    本节知识点 1.__get__, __getattr__, __getattribute__的区别 2.__getattr__巧妙应用 3.延迟初始化(lazy property) 1.__get__ ...

  5. Python的__getattr__和__getattribute__

    __getattr____getattr__在当前主流的Python版本中都可用,重载__getattr__方法对类及其实例未定义的属性有效.也就属性是说,如果访问的属性存在,就不会调用__getat ...

  6. python魔法方法:__getattr__,__setattr__,__getattribute__

    python魔法方法:__getattr__,__setattr__,__getattribute__ 难得有时间看看书....静下心来好好的看了看Python..其实他真的没有自己最开始想的那么简单 ...

  7. python中的__getattr__、__getattribute__、__setattr__、__delattr__、__dir__

    __getattr__:     属性查找失败后,解释器会调用 __getattr__ 方法. class TmpTest: def __init__(self): self.tmp = 'tmp12 ...

  8. 一篇文章让你彻底明白__getattr__、__getattribute__、__getitem__的用法与执行原理

    __getattr__ 在Python中,当我们试图访问一个不存在的属性的时候,会报出一个AttributeError.但是如何才能避免这一点呢?于是__getattr__便闪亮登场了 当访问一个不存 ...

  9. 一些代码 II (ConfigParser、创建大文件的技巧、__getattr__和__getattribute__、docstring和装饰器、抽象方法)

    1. ConfigParser format.conf [DEFAULT] conn_str = %(dbn)s://%(user)s:%(pw)s@%(host)s:%(port)s/%(db)s ...

随机推荐

  1. 用apscheduler写python定时脚本

    apscheduler 官方文档:http://apscheduler.readthedocs.io/en/latest/ 写一个后台定时任务,一般2个选择,一个是apscheduler,一个cele ...

  2. UOJ 34: 多项式乘法(FFT模板题)

    关于FFT 这个博客的讲解超级棒 http://blog.miskcoo.com/2015/04/polynomial-multiplication-and-fast-fourier-transfor ...

  3. 题解【BZOJ4145】「AMPPZ2014」The Prices

    题目描述 你要购买 \(m\) 种物品各一件,一共有 \(n\) 家商店,你到第 \(i\) 家商店的路费为 \(d[i]\),在第 \(i\) 家商店购买第 \(j\) 种物品的费用为 \(c[i] ...

  4. HDU 3530

    新手理解(可能有理解错误的地方,请指教,嘿嘿) #include<stdio.h> #include<string.h> #include<math.h> #inc ...

  5. python记之Hello world!

    ________________________________该动手实践了. 数和表达式 交互式Python解释器可用作功能强大的计算器. 除法运算的结果为小数,即浮点数(float或floatin ...

  6. party lamps(dfs优化+规律枚举)

    Problem description: To brighten up the gala dinner of the IOI'98 we have a set of N coloured lamps ...

  7. 网关集成Swagger出现404错误

    原因是忘了在需要生成api的类上加入注解  @EnableSwagger2Doc

  8. jmeter的使用---用户变量

    用户变量有以下方式: 一.外部引入:csv引入参数 二.sample传递参数:http请求的body参数 三.定义用户变量:全局变量 (1)用户定义的变量 name,和value   一对一 (2)用 ...

  9. FreeRTOS学习笔记3:内核控制及开启调度器

    内核控制函数API 应用层中不会用到taskYIELD() //任务切换.会自动切换当前就绪表里优先级最高的任务 临界区 //不能被打断的代码段任务中进入临界区任务中退出临界区中断服务进入临界区中断服 ...

  10. ACM-ICPC实验室20.2.22测试-动态规划

    C.田忌赛马 直接贪心做就可以~ #include<bits/stdc++.h> using namespace std; ; int a[maxn],b[maxn]; int main( ...