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__ 举例的更多相关文章

  1. [Python3 填坑] 006 “杠零”,空字符的使用

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 \0 是空字符,输出时看不到它,但它占 1 个字符的长度 2.2 \0 "遇八进制失效" 2.3 \0 与 '' 不 ...

  2. [Python3 填坑] 004 关于八进制

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 2.2.1 先说结论 2.2.2 八进制的用途 2.2.3 少废话,上例子 1. print( 坑的信息 ...

  3. [Python3 填坑] 001 格式化符号 & 格式化操作符的辅助指令

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python 格式化符号表 举例说明 (1) %c (2) %s 与 %d (3) %o (4) %x (5) %f (6) %e (7 ...

  4. [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...

  5. [Python3 填坑] 009 深拷贝与浅拷贝

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python3.7 官方文档 2.2 赋值.切片与 copy() 分析 分析 分析 分析 2.3 copy 模块 分析 分析 2.4 小 ...

  6. [Python3 填坑] 005 如何“响铃”

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 问题的解决 1. print( 坑的信息 ) 挖坑时间:2019/01/08 明细 坑的编码 内容 Py004-2 ...

  7. [Python3 填坑] 003 关键字?保留字?预留字?

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 问题的由来 2.2 网上搜索 2.3 结论 2.4 后记 1. print( 坑的信息 ) 挖坑时间:2019/01/04 明细 坑的编 ...

  8. [Python3 填坑] 018 组装类的几个例子

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 MetaClass 举例 2.2 type 举例 2.3 MetaClass 举例 1. print( 坑的信息 ) 挖坑时间:2019 ...

  9. [Python3 填坑] 017 实例方法、静态方法、类方法的区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 先上例子 2.2 分析 1. print( 坑的信息 ) 挖坑时间:2019/04/07 明细 坑的编码 内容 Py024-1 实例方法 ...

随机推荐

  1. [lean scala]|How to create a SBT project with Intellij IDEA

    this article show you how to create a SBT project with IDEA. prerequisite: 1.JDK8 2.Scala 2.11.8 3.I ...

  2. GUI学习之二十四——QDialog学习总结

    今天学习对话框输入控件的基类(QDialog). 一.描述 是对话类窗口(字体框.颜色选择.文件选择框等)的基类. 对话框窗口是顶级窗口(就是说不包含于哪个父类的显示界面里),主要用于短期任务和与用户 ...

  3. 软件工程大作业(学生会管理系统)Web端个人总结报告

    软件工程大作业(学生会管理系统)Web端个人总结报告 一.小组信息 1.所在小组:第二组 2.小组选题:学生会管理系统 3.项目源代码链接: Web端源代码:code 小程序端源代码:code APP ...

  4. RAC heartbeat 心跳机制

    世界上最遥远的距离,不是生与死.而是我们同一个集群的两个节点,你却听不到我的心跳. 必要性:维持集群的⼀致性RAC⼼跳机制 – 集群⼼跳基本机制:1.确定节点和节点间的连通性,达到彼此了解2.⽤共享的 ...

  5. Flask【第2篇】:Flask基础

    Flask基础 知识点回顾 1.flask依赖wsgi,实现wsgi的模块:wsgiref,werkzeug,uwsgi 2.实例化Flask对象,里面是有参数的 app = Flask(__name ...

  6. python数组中在某一元素前插入数据

    # 已知有一个已经排好序的数组.要求是,有一个新数据项,要求按原来的规律将它插入数组中. a=[1,2,3,4,5,6,7,8,9]num=int(input("input num:&quo ...

  7. 【GDOI 2016 Day2】第一题 SigemaGO

    题目 分析 拆点连边+spfa. 首先把图分成2lim+1层,也就是每个点拆成2lim+1个点. 如果a和b之间.b和c有一条有向边,那么连边(k,a)-->(k+1,b),(k+1,b)--& ...

  8. spark(2.1.0) 操作hbase(1.0.2)

    一.写操作 1.spark中引入外部jar包 1)创建/usr/software/spark_jars目录,把hbase里的lib里的以下七个jar放入/usr/software/spark_jars ...

  9. Linux GDB 程序调试工具使用详解

    转自    http://www.codeceo.com/article/linux-gdb-tools.html 整理的挺全的 GDB概述 GDB是GNU开源组织发布的一个强大的UNIX下的程序调试 ...

  10. SQL 查询表字段长度, 名称, 类型, 存储过程创建和修改时间

    获取存储过程的修改时间和创建时间查询建立时间 --表 select * from sysobjects where id=object_id(N'表名') and xtype='U' --表的结构 s ...