python __setattr__、__getattr__、__getattribute__全面详解
一、属性引用函数
hasattr(obj,name[,default])
getattr(obj,name)
setattr(obj,name,value)
delattr(obj,name)
二、属性引用重载
def __setattr__(self,key,value):
1.拦截所有属性的赋值语句。
2.self.attr=value 相当于 self.__setattr__("attr",value)。
3.如果在__setattr__中对任何self属性赋值,都会再调用__setattr__,导致无穷递归循环。只能self.__dict__["attr"]=value 。
def __getattribute__(self, key):
1.拦截所有的属性获取,包括未定义的属性,self.__dict__,等点号运算。
2.所有的属性先在__getattribute__中没有找到,就会抛出AttributeError,__getattr__接收这个错误,此时进入__getattr__中继续寻找。
3.如果__getattribute__没有抛出AttributeError,将不会调用__getattr__。
def __getattr__(self, key):
拦截self.attr运算。当在__dict__中未找到该属性时,在类属性中也没有找到该属性,并且在继承树中也没有找到该属性,就会调用这个方法。
def __delattr__(self,key): 删除属性
三、示例
class Square: # 正方形
def __init__(self, l):
self.length = l # 边长
def __getattr__(self, key):
if key == "area":
return "__getattr__被调用了,为了area"
sq = Square(10)
print(sq.length) #
print(sq.area) # __getattr__被调用了,为了area
class Square: # 正方形
def __init__(self, l):
pass
def __getattr__(self, key):
print("__getattr__被调用了")
if key == "length":
return 1111
def __getattribute__(self, key123):
print("__getattribute__被调用了")
# return 123456
raise AttributeError
sq = Square(10)
print(sq.length)
# __getattribute__被调用了
# __getattr__被调用了
#
class Square: # 正方形
def __init__(self,l):
pass
def __getattr__(self, key):
print("__getattr__被调用了")
raise AttributeError("")
def __getattribute__(self, key123):
print("__getattribute__被调用了")
return 123456
# raise AttributeError
sq = Square(10)
print(sq.length)
# __getattribute__被调用了
#
class Square: # 正方形
def __init__(self, l):
self.length = l # 边长
def __setattr__(self, key, value):
print("调用__setattr__", "key=", key)
if key == "perimeter":
self.__dict__["length"] = value / 4
self.__dict__["perimeter"] = value
if key == "length":
self.__dict__["length"] = value
self.__dict__["perimeter"] = value * 4
def __getattr__(self, key):
print("调用__getattr__ ,", "key =", key)
if key == "area":
return 960
def __getattribute__(self, key123):
print("调用__getattribute__ ,", "key123 =", key123)
return object.__getattribute__(self, key123)
sq = Square(10)
# 调用__setattr__
# 调用__getattribute__ , key123 = __dict__ 此时执行self.__dict__["length"] = value
# 调用__getattribute__ , key123 = __dict__ 此时执行self.__dict__["perimeter"] = value * 4
print(sq.length)
# 调用__getattribute__ , key123 = length 此时执行self.length = l # 边长
print(sq.perimeter)
# 调用__getattribute__ , key123 = perimeter
#
print(sq.area)
# 调用__getattribute__ , key123 = area
# 调用__getattr__ , key = area
#
python __setattr__、__getattr__、__getattribute__全面详解的更多相关文章
- 【Python】Python内置函数dir详解
1.命令介绍 最近学习并使用了一个python的内置函数dir,首先help一下: 复制代码代码如下: >>> help(dir)Help on built-in function ...
- python __setattr__, __getattr__, __delattr__, __call__
python __setattr__, __getattr__, __delattr__, __call__ getattr `getattr`函数属于内建函数,可以通过函数名称获取 value = ...
- Python安装、配置图文详解(转载)
Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境(I ...
- 【和我一起学python吧】Python安装、配置图文详解
Python安装.配置图文详解 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 三. 在windows下配置python集成开发环境( ...
- Python中的高级数据结构详解
这篇文章主要介绍了Python中的高级数据结构详解,本文讲解了Collection.Array.Heapq.Bisect.Weakref.Copy以及Pprint这些数据结构的用法,需要的朋友可以参考 ...
- [转]使用python来操作redis用法详解
转自:使用python来操作redis用法详解 class CommRedisBase(): def __init__(self): REDIS_CONF = {} connection_pool = ...
- Python中格式化format()方法详解
Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参 ...
- python设计模式之装饰器详解(三)
python的装饰器使用是python语言一个非常重要的部分,装饰器是程序设计模式中装饰模式的具体化,python提供了特殊的语法糖可以非常方便的实现装饰模式. 系列文章 python设计模式之单例模 ...
- Python调用windows下DLL详解
Python调用windows下DLL详解 - ctypes库的使用 2014年09月05日 16:05:44 阅读数:6942 在python中某些时候需要C做效率上的补充,在实际应用中,需要做部分 ...
- Python操作redis字符串(String)详解 (三)
# -*- coding: utf-8 -*- import redis #这个redis不能用,请根据自己的需要修改 r =redis.Redis(host=") 1.SET 命令用于设置 ...
随机推荐
- poj2718 Smallest Difference(dfs+特判,还可以贪心更快)
https://vjudge.net/problem/POJ-2718 其实不太理解为什么10超时了.. 这题似乎是有贪心优化的方法的,我下面直接暴力了.. 暴力之余要特判两个点:1.超时点就是n=1 ...
- Deepin 15.4 升级 chrome flash
到 adobe 官方下载 flash插件 flash_player_ppapi_linux ~/.config/google-chrome/PepperFlash下建个目录 23.0.0.185,把 ...
- 控制WinForm中Tab键的跳转
一,需求 在Winform中,默认情况下,按下Tab键,光标会按照我们设定的TabIndex值从小到大进行跳转. 但如果用户要求按下Tab键跳转到特定的控件,这种要求还是很合理的,比如用户只想输入几个 ...
- 设备树(device tree)学习笔记
作者信息 作者:彭东林 邮箱:pengdonglin137@163.com 1.反编译设备树 在设备树学习的时候,如果可以看到最终生成的设备树的内容,对于我们学习设备树以及分析问题有很大帮助.这里我们 ...
- 搜索历史命令 Ctrl + R ( ctrl + r to search the history command )
Linux下的神器 ctrl + r (reverse-i-search ) 的使用方法: (reverse-i-search usage: ) (press ctl + r ) 输入任意字符,例 ...
- 06、action操作开发实战
1.reduce: 2.collect: 3.count: 4.take: 5.saveAsTextFile: 6.countByKey: 7.foreach: package sparkcore.j ...
- h264_rtp打包解包类及实现demo
打包头文件: class CH2642Rtp { public: CH2642Rtp(uint32_t ssrc, uint8_t payloadType = 96, uint8_t fps = 25 ...
- HashTable代码解析
HashTable继承关系如下: HashTable是一个线程安全的[键-值对]存储结构.其存储结构和HashMap相同,参考这里. 1. HashTable定义了一个类型为Entry<K,V& ...
- 使用Sphinx编写文档
操作系统 : Windows7_x64 Python 版本 : 2.7.10 Sphinx 版本 : 官方网址:http://sphinx-doc.org github地址: https://gith ...
- maven error: element dependency can not have character children
就是Mavn pom.xml的解析错误,因为dependency这个标签中有不可见的垃圾字符,解决方法就是删掉重新打字进去就可以了. references: https://stackoverflow ...