[Python3 填坑] 016 对 __getattr__ 和 __setattr__ 举例
1. print( 坑的信息 )
- 挖坑时间:2019/04/07
- 明细
| 坑的编码 | 内容 |
|---|---|
| Py023-4 | 对 __getattr__ 和 __setattr__ 举例 |
2. 开始填坑
2.1 _getattr_
- Python 3.7.3 官方文档
Called when the default attribute access fails with an AttributeError (either __getattribute__() raises an AttributeError because name is not an instance attribute or an attribute in the class tree for self; or __get__() of a name property raises AttributeError). This method should either return the (computed) attribute value or raise an AttributeError exception.
Note that if the attribute is found through the normal mechanism, __getattr__() is not called. (This is an intentional asymmetry between __getattr__() and __setattr__().) This is done both for efficiency reasons and because otherwise __getattr__() would have no way to access other attributes of the instance. Note that at least for instance variables, you can fake total control by not inserting any values in the instance attribute dictionary (but instead inserting them in another object). See the __getattribute__() method below for a way to actually get total control over attribute access.
- 少废话,上例子
# 1.0
>>> class A(object):
... pass
...
>>> a = A()
>>> a.name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'name'
>>>
# 2.0
>>> class A(object):
... def __getattr__(self, name):
... print("no way, haha")
...
>>> a = A()
>>> a.name
no way, haha
>>>
- 小节
- 在实例对象进行“点操作”时,会自动调用
__getattr__
- 在实例对象进行“点操作”时,会自动调用
2.2 _setattr_
- Python 3.7.3 官方文档
object.__setattr__(self, name, value)
Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). name is the attribute name, value is the value to be assigned to it.
If __setattr__() wants to assign to an instance attribute, it should call the base class method with the same name, for example, object.__setattr__(self, name, value).
- 少废话,上例子
# 1.0
class A(object):
def __init__(self, num):
self.num = num
a = A(20)
print(a.num)
a.num = 30
print(a.num)
a.__setattr__('num', 40)
print(a.num)
>>>
20
30
40
# 2.0
class A(object):
def __setattr__(self, num, value):
print("----setattr")
#self.num = value # 死循环
super().__setattr__(num, value)
a = A()
print('1.')
a.num = 18
print('2.')
print(a.num)
>>>
1.
----setattr
2.
18
[Python3 填坑] 016 对 __getattr__ 和 __setattr__ 举例的更多相关文章
- [Python3 填坑] 006 “杠零”,空字符的使用
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...
- [Python3 填坑] 004 关于八进制
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...
- [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...
- [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...
- [Python3 填坑] 009 深拷贝与浅拷贝
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...
- [Python3 填坑] 005 如何“响铃”
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 1. print( 坑的信息 ) 挖坑时间:2019/01/08 明细 坑的编码 内容 Py004-2 ...
- [Python3 填坑] 003 关键字?保留字?预留字?
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 网上搜索 2.3 结论 2.4 后记 1. print( 坑的信息 ) 挖坑时间:2019/01/04 明细 坑的编 ...
- [Python3 填坑] 018 组装类的几个例子
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 MetaClass 举例 2.2 type 举例 2.3 MetaClass 举例 1. print( 坑的信息 ) 挖坑时间:2019 ...
- [Python3 填坑] 017 实例方法、静态方法、类方法的区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 先上例子 2.2 分析 1. print( 坑的信息 ) 挖坑时间:2019/04/07 明细 坑的编码 内容 Py024-1 实例方法 ...
随机推荐
- GUI学习之十二——QTextEdit学习总结
在学习了QFrame和QAbstractScrollArea两个父类后,接下来是一个重头戏:QTextEdit. 一.QTextEdit特性 QTextEdit是一个高级的WYSIWYG(What Y ...
- 使用QT创建系统托盘
使用QT来创建一个系统托盘,事实上是一件很简单的事.为什么这么说?一是因为QT文档给出了比较详细的例子,二是QT的结构比较优雅,设计风格统一.但是在动手之前,我们要从哪里下手?虽然QT文档给出了一个比 ...
- QLCDNumber
继承于 QFrame 展示LCD样式的数字,它可以显示几乎任何大小的数字,它可以显示十进制,十六进制,八进制或二进制数 能够展示的字符: 0/O, 1, 2, 3, 4, 5/S, 6, 7, 8, ...
- Mac升级系统后 Pod Install报错-不能用 解决办法
brew reinstall cocoapods brew install ruby brew link --overwrite cocoapods 最近将Mac OS升级到10.15.1 ,再回来初 ...
- Seata简单介绍及其原理(一)
Seata 是什么? Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务.Seata 将为用户提供了 AT.TCC.SAGA 和 XA 事务模式,为用户打造一站式的 ...
- 【bzoj3566】 [SHOI2014]概率充电器
*题目描述: 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器: “采用全新纳米级加工技术,实现元件与导线能否通电完全由真随机数决定!SHOI 概率充电器,您生活不可 ...
- 【CF1252G】Performance Review(线段树)
题意: n,q<=1e5,a[i],b[i][j]<=1e9,保证能力值互不相同,询问之间保留前面的影响 思路:其实把大于a[1]的看成0,小于的看成1,设第i天小于a[1]的有b[i]个 ...
- Packet Transactions: High-level Programming for Line-Rate Switches
Name of article:Packet Transactions: High-level Programming for Line-Rate Switches Origin of the art ...
- 解决Acunetix 12中文汉化的方法
最近下载一款测试软件acunetix,苦于满屏英文的苦恼,看不懂,于是乎就问度娘,结果度娘就是给中文破解包: 我是12版的,网上提供的都是11版的,没法用.怎么办呢?还好我是做测试的,平时做兼容性测试 ...
- iOS模拟器Custom Location被重置解决方案
转自王中周的技术博客 问题说明 在做地图类应用时,经常需要用到位置模拟功能.iOS模拟器提供了该功能,我们可以设置指定的经纬度,选中模拟器后,按照以下菜单层次进入即可设置: Debug --> ...