[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 实例方法 ...
随机推荐
- TFServering和docker
nvidia-docker安装:https://github.com/NVIDIA/nvidia-docker nvidia-docker的基本操作:https://blog.csdn.net/pan ...
- 基于iview使用jsx扩展成可编辑的表格
<template> <div> <Table :columns="columns" :data="data"></T ...
- Scrat
前端工程化集成:Scrat UC团队在百度FIS基础上二次开发的模块化开发框架,可以与vue配合使用
- 使用Hybris Commerce User API读取用户信息时,电话字段没有返回
在使用Hybris Commerce User API读取一个user信息时,我遇到一个问题,在API返回的结构里没有包含期望看到的Phone字段. 仔细观察Swagger里对response结构的说 ...
- linux运维、架构之路-KVM虚拟化技术
一.云计算概述 云计算:是一种资源使用和交付模式 虚拟化:一种具体的技术,用来将物理机虚拟成为多个相互独立的虚拟机.云计算不等于虚拟化,云计算是使用了虚拟化的技术做支撑 二.KVM配置使用 1.系统环 ...
- linux运维、架构之路-linux基础知识
1.PATH环境变量 LANG ——————>变量名字 $LANG ——————>查看变量内容 LANG= ——————>修改变量 系统环境变量填加内容前面使用export expo ...
- CSS3制作太极图以及用JS实现旋转太极图
太极图可以理解为一个一半黑一半白的半圆,上面放置着两个圆形,一个黑色边框白色芯,一个白色边框黑色芯. 1.实现黑白各半的圆形. .box{ width:200px;height:200px; bor ...
- Apache简介
1.什么是Apache 注:Apache是目前使用最广泛的Web服务器软件. 2.发展历史 注:客户端mosaic程序是Netscape浏览器的前身,后来演变成mozilla浏览器,即我们使用的Fir ...
- Leetcode 2. Add Two Numbers(指针和new的使用)结构体指针
---恢复内容开始--- You are given two non-empty linked lists representing two non-negative integers. The di ...
- UE4 质心相关
Add force 始终给质心施加力,即使手动偏移了质心(Center of mass)也不例外. Add force at location local 相对于物体的原始质心位置增加一个偏移值后的位 ...