传送门

  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. 【C语言】输入10个人的成绩,求平均值

    #include<stdio.h> int main() { int i; ,score[]; printf("请输入10个数字:\n"); ; i < ; i+ ...

  2. nmon+python 基于AIX系统数据分析

    https://sourceforge.net/projects/pynmongraph/ github :https://github.com/madmaze/pyNmonAnalyzer nmon ...

  3. docker镜像 - 下载、创建镜像和导入导出镜像

    实验环境 CentOS 7.5 安装并启动docker yum install -y docker systemctl start docker 镜像 安装镜像 docker pull [OPTION ...

  4. Docker 基本命令和使用

    Docker 基本命令 systemctl start docker : 启动 Docker systemctl stop docker : 停止 Docker systemctl restart d ...

  5. .NTE Core Web API Example

    Source from :https://www.codeproject.com/Articles/1260600/Speed-up-ASP-NET-Core-WEB-API-application- ...

  6. 8.7-Day1T1

    题目大意: T组测试数据,每组测试数据给出一个n,求[0,n-1]所有逆元的和.(n可能不为质数) 题解: 我的想法: 求出每一个数的逆元,再相加.由于有n为质数的时候,所以,我将它分为两种情况:(1 ...

  7. android:Android 6.0权限控制代码封装

    新建的Activity类可以继承这个Activity,这个类封装了关于新版的权限处理相关的代码 使用方法: package com.glsite.phone; import android.conte ...

  8. bugku 多种方法解决

    首先打开链接发现是一个exe文件 实在是想不出办法了 只能回去看提示 说会有一张图片 不知道怎么就打开了hxd 然后拖进去 发现了一串 用图片base64加密的码 然后在网页中找到 解码 工具 htt ...

  9. 动态调整线程数的python爬虫代码分享

    这几天在忙一个爬虫程序,一直在改进他,从一开始的单线程,好几秒一张图片(网络不好),,,到现在每秒钟十几张图片,,, 四个小时586万条数据,,,简直不要太爽 先上图 最终写出来的程序,线程数已经可以 ...

  10. LUA利用第三方API访问数据库

    ===========数据库访问--第三方 http { upstream backend { drizzle_server 192.168.4.119:3306 protocol=mysql dbn ...