[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 实例方法 ...
随机推荐
- Codeforces Round #573 (Div. 2) E. Tokitsukaze and Duel (博弈)
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 基于ldap+sentry+rbac的hive数据库权限测试
1.rbac系统简介 2.sentry系统简介 3.ldap系统简介 4.整个待测系统简介 user-role=group-role user-role是用户在rbac系统创建项目以及把项目成员以及给 ...
- springboot 初探 、基础及配置
一.spring boot 的核心功能 独立运行的spring项目.内嵌servlet容器.提供starter简化maven配置.自动配置Spring.准生产的应用监控.无代码生成和xml配置二.sp ...
- 双层for循环用java中的stream流来实现
//双重for循环for (int i = 0; i < fusRecomConfigDOList.size(); i++) { for (int j = 0; j < fusRecomC ...
- [CF1142E] Pink Floyd
传送门 题意:一个\(n\)个点的竞赛图,给出\(m\)条红色的边,其方向确定,其余边均为绿色,方向未知.你可以询问不超过\(2n\)次,每次询问一条绿色边的方向.要求找到一个点\(x\),使得\(x ...
- 【leetcode】1138. Alphabet Board Path
题目如下: On an alphabet board, we start at position (0, 0), corresponding to character board[0][0]. Her ...
- 安卓手机通过有线连接PC上网
因手机wifi坏了,速度上限为2.5m/s.无法发挥出100m带宽的威力. 这里探索各大神的指导,记录下其中一种方式. :链接 主题:使用openvpn和手机的USB共享网络 通过mico ...
- vue-cli3.0以上项目中引入jquery的方法
这里配置的是vue-cli3.0引入jquery的方法,不是vue-cli2.0的配置方法 一.安装jquery npm install jquery --save 二.在vue.config.js ...
- IDEA maven 配置,运行比较慢,加截本地仓库资源数据
在 Runner 配置了参数: -DarchetypeCatalog=internal
- mysql DISTINCT语句 语法
mysql DISTINCT语句 语法 作用:用于返回唯一不同的值. 语法:SELECT DISTINCT 列名称 FROM 表名称.扬州大理石量具 mysql DISTINCT语句 示例 //从表中 ...