Pthon魔术方法(Magic Methods)-反射
Pthon魔术方法(Magic Methods)-反射
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.反射概述
运行时,区别于编译时,指的时程序被加载到内存中执行的时候。 反射,reflection,指的时运行时获取类型定义信息。 一个对象能够再运行时,像照镜子一样,反射出其类型信息。 简单的说,再python这种,能够通过一个对象,找出其type,class,attribute或method的能力,称为反射或者自省。 具有反射能力的函数有type(),isinstance(),callable(),dir(),getattr()等。
二.反射相关的函数和方法
1>.看一个简单需求
有一个Point类,查看它的实例的属性,并修改它。动态为实例增加属性
#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie class Point:
def __init__(self,x,y):
self.x = x
self.y = y def __str__(self):
return "Point({},{})".format(self.x,self.y) def show(self):
print(self.x,self.y) p = Point(10,20)
print(p)
print(p.__dict__)
p.__dict__['y'] = 30
print(p.__dict__)
p.z = 10
print(p.__dict__)
print(dir(p))
print(p.__dir__())
Point(10,20)
{'x': 10, 'y': 20}
{'x': 10, 'y': 30}
{'x': 10, 'y': 30, 'z': 10}
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'show', 'x', 'y', 'z']
['x', 'y', 'z', '__module__', '__init__', '__str__', 'show', '__dict__', '__weakref__', '__doc__', '__repr__', '__hash__', '__getattribute__', '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__new__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__']
以上代码执行结果戳这里
2>.Python提供了内置的反射函数
getattr(object,name[,default])
通过name返回object的属性值。当属性不存在,将使用default返回,如果没有default,则抛出AttributeError。name必须为字符串。 setattr(object,name,value)
object的属性存在,则覆盖,不存在,则新增。 hasattr(object,name)
判断对象是否有这个名字的属性,name必须为字符串。 用上面的方法来修改上例的代码,具体代码如下:
#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie class Point:
def __init__(self,x,y):
self.x = x
self.y = y def __str__(self):
return "Point({},{})".format(self.x,self.y) __repr__ = __str__ def show(self):
print(self.x,self.y) p1 = Point(10,20)
p2 = Point(50,100) print(repr(p1),repr(p2),sep="\n")
print(p1.__dict__)
setattr(p1,"y",33)
setattr(p1,"z",66)
print(getattr(p1,"__dict__")) #动态调用方法
if hasattr(p1,"show"):
getattr(p1,"show")() #动态为类增加方法
if not hasattr(Point,"add"):
setattr(Point,"add",lambda self,other:Point(self.x + other.x,self.y + other.y)) print(Point.add)
print(p1.add) #绑定方法
print(p1.add(p2)) #由于是绑定方法,因此只需要给add传递一个参数即可 #为实力增加方法
if not hasattr(p1,"sub"):
setattr(p1,"sub",lambda self,other:Point(self.x - other.x,self.y - other.y)) print(p1.sub) #未绑定方法
print(p1.sub(p1,p1)) #观察add咋子在谁里面,sub在谁里面
print(p1.__dict__)
print(Point.__dict__)
Point(10,20)
Point(50,100)
{'x': 10, 'y': 20}
{'x': 10, 'y': 33, 'z': 66}
10 33
<function <lambda> at 0x00000151BA593708>
<bound method <lambda> of Point(10,33)>
Point(60,133)
<function <lambda> at 0x00000151BA5A5438>
Point(0,0)
{'x': 10, 'y': 33, 'z': 66, 'sub': <function <lambda> at 0x00000151BA5A5438>}
{'__module__': '__main__', '__init__': <function Point.__init__ at 0x00000151BA5A5678>, '__str__': <function Point.__str__ at 0x00000151BA5A51F8>, '__repr__': <function Point.__str__ at 0x00000151BA5A51F8>, 'show': <function Point.show at 0x00000151BA5A5318>, '__dict__': <attribute '__dict__' of 'Point' objects>, '__weakref__': <attribute '__weakref__' of 'Point' objects>, '__doc__': None, 'add': <function <lambda> at 0x00000151BA593708>}
以上代码执行结果戳这里
3>.这种动态增加属性的方式和装饰器修饰一个类,Mixin方式的差异在哪?
这种动态增删属性的方式是运行时来改变类或者实例的方式,但是装饰器或Mixin都是定义时就决定了,因此反射能力具有更大的灵活性。
三.反射相关的魔术方法
1>.反射相关的魔术方法概述
__getattr__():
当通过搜索实例,实例的类及在祖先类查不到属性,就会调用此方法。 __setattr__():
通过"."访问实例属性,进行增加,修改都要调用它。 __delattr__():
当通过实例来删除属性时调用此方法。 __getattribute():
实例所有的属性调用都从这个方法开始。 属性查找顺序:
实例调用__getattribute__() ---> instance.__dict__ ---> instance.__class__.__dict__ ---> 继承的祖先类(知道object)的__dict__ ---> 调用__getattr__()
2>."__getattr__()"方法测试
#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie class Base:
m = 100 class Point(Base):
n = 200 def __init__(self,x,y):
self.x = x
self.y = y def __str__(self):
return "Point({},{})".format(self.x,self.y) def __getattr__(self, item):
return "missing {}".format(item) p1 = Point(10,20)
print(p1.x)
print(p1.n)
print(p1.m)
print(p1.t) #实例属性会按照继承关系找,如果找不到,就会执行"__getattr__()"方法,如果没有这个方法,就会抛出AttributeError异常表示找不到属性。
10
200
100
missing t
以上代码执行结果戳这里
3>."__setattr__()"方法测试
#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie class Base:
m = 100 class Point(Base):
n = 200 def __init__(self,x,y):
self.x = x
self.y = y def __str__(self):
return "Point({},{})".format(self.x,self.y) def __getattr__(self, item):
return "missing {}".format(item) def __setattr__(self, key, value):
"""
实例通过点号(".")设置属性,例如"self.x = x"属性赋值,就会调用"def __setattr__(self, key, value)"方法,该方法可用拦截对实例属性的增加,修改操作,如果要设置生效,需要自己操作实例的"__dict__"。
很显然,我们在"__setattr__"方法中并没有给修改实例的"__dict__",而是仅仅时执行了打印语句,因此实例的"__dict__"中默认不会存在任何属性。
"""
print("setattr {} = {}".format(key,value)) p1 = Point(10,20) #实例化过程中会调用"__init__"方法,而"__init__"方法存在"self.x = x"的语句,因此每当执行类似语句都会调用"__setattr__"方法
print(p1.x) #如上所述,"def __setattr__(self, key, value)"并没有将"x"属性加入到实例字典中,且p1实例的类和父类Base以及Object类都没有"x"属性,因此在这里会调用"__getattr__"方法。
print(p1.n)
print(p1.m)
print(p1.t) #实例属性会按照继承关系找,如果找不到,就会执行"__getattr__()"方法,如果没有这个方法,就会抛出AttributeError异常表示找不到属性。 p1.x = 50
print(p1.x)
print(p1.__dict__)
p1.__dict__["x"] = 100 #实例属性要加到实例的"__dict__"中才能访问到,既然"def __setattr__(self, key, value)"没有帮我们完成这样的操作,我们就需要自己完成
print(p1.__dict__)
print(p1.x) #由于将"x"属性加入到实例字典中,因此我们可用访问到"x"属性啦!
setattr x = 10
setattr y = 20
missing x
200
100
missing t
setattr x = 50
missing x
{}
{'x': 100}
100
以上代码执行结果戳这里
4>."__delattr__()"方法测试
#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie class Base:
m = 100 class Point(Base):
n = 200
d = {}
def __init__(self,x,y):
self.x = x
setattr(self,"y",y)
self.__dict__["a"] = 50 def __str__(self):
return "Point({},{})".format(self.x,self.y) def __getattr__(self, item):
print( "missing {}".format(item))
return self.d[item] def __setattr__(self, key, value):
print("setattr {} = {}".format(key,value))
self.d[key] = value def __delattr__(self, item):
"""
可用阻止通过实例来删除属性的操作。
"""
print("Can not del {}".format(item)) def show(self):
print(self.x,self.y) p1 = Point(10,20)
p1.show()
print(p1.a)
print(p1.__dict__)
print(Point.__dict__)
del p1.x
p1.q = 15
print(p1.__dict__)
del p1.q
del p1.n
del Point.n
print(Point.__dict__) #可用阻止通过实例来删除属性的操作,但是通过类依然可以删除
setattr x = 10
setattr y = 20
missing x
missing y
10 20
50
{'a': 50}
{'__module__': '__main__', 'n': 200, 'd': {'x': 10, 'y': 20}, '__init__': <function Point.__init__ at 0x00000276AEF35318>, '__str__': <function Point.__str__ at 0x00000276AEF35438>, '__getattr__': <function Point.__getattr__ at 0x00000276AEF354C8>, '__setattr__': <function Point.__setattr__ at 0x00000276AEF35558>, '__delattr__': <function Point.__delattr__ at 0x00000276AEF358B8>, 'show': <function Point.show at 0x00000276AEF35948>, '__doc__': None}
Can not del x
setattr q = 15
{'a': 50}
Can not del q
Can not del n
{'__module__': '__main__', 'd': {'x': 10, 'y': 20, 'q': 15}, '__init__': <function Point.__init__ at 0x00000276AEF35318>, '__str__': <function Point.__str__ at 0x00000276AEF35438>, '__getattr__': <function Point.__getattr__ at 0x00000276AEF354C8>, '__setattr__': <function Point.__setattr__ at 0x00000276AEF35558>, '__delattr__': <function Point.__delattr__ at 0x00000276AEF358B8>, 'show': <function Point.show at 0x00000276AEF35948>, '__doc__': None}
以上代码执行结果戳这里
5>."__getattribute__()"方法测试
#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie class Base:
m = 100 class Point(Base):
n = 200
def __init__(self,x,y):
self.x = x
setattr(self,"y",y) def __getattr__(self, item):
print( "missing {}".format(item))
return self.d[item] def __getattribute__(self, item):
"""
实例的所有的属性访问,第一个都会调用"def __getattribute__(self, item)"方法,它阻止了属性的查找,该方法应该返回(计算后的)值或者抛出一个AttributeError异常。
它的return值将作为属性查找的结果
如果抛出AttributeError异常,则会直接调用"__getattr__"方法,因为表述属性没有找到。 温馨提示:
"__getattribute__"方法中为了避免在该方法中无线的递归,它的实现应该永远调用基类的同名方法以访问需要的任何属性,例如"object.__getattribute__(self,name)"。
注意,除非你明确地知道"__getattribute__"方法用来做什么,否则不要使用它。
"""
return item p1 = Point(10,20)
print(p1.__dict__)
print(p1.x)
print(p1.n)
print(p1.n)
print(p1.t)
print(Point.__dict__)
print(Point.n)
__dict__
x
n
n
t
{'__module__': '__main__', 'n': 200, '__init__': <function Point.__init__ at 0x00000230DA543708>, '__getattr__': <function Point.__getattr__ at 0x00000230DA555678>, '__getattribute__': <function Point.__getattribute__ at 0x00000230DA5551F8>, '__doc__': None}
200
以上代码执行结果戳这里
Pthon魔术方法(Magic Methods)-反射的更多相关文章
- php中的魔术方法(Magic methods)和魔术常亮
PHP中把以两个下划线__开头的方法称为魔术方法,这些方法在PHP中充当了举足轻重的作用. 魔术方法包括: __construct(),类的构造函数 __destruct(),类的析构函数 __cal ...
- Pthon魔术方法(Magic Methods)-描述器
Pthon魔术方法(Magic Methods)-描述器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.描述器概述 1>.描述器定义 Python中,一个类实现了&quo ...
- Pthon魔术方法(Magic Methods)-上下文管理
Pthon魔术方法(Magic Methods)-上下文管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.上下文管理方法 __enter__: 进入与此对象相关的上下文.如果 ...
- Pthon魔术方法(Magic Methods)-可调用对象
Pthon魔术方法(Magic Methods)-可调用对象 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.可调用对象方法 __call__: 类中定义一个该方法,实例就可以像 ...
- Pthon魔术方法(Magic Methods)-容器相关方法
Pthon魔术方法(Magic Methods)-容器相关方法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.容器相关方法汇总 __len__: 内建函数len(),返回对象的 ...
- Pthon魔术方法(Magic Methods)-运算符重载
Pthon魔术方法(Magic Methods)-运算符重载 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Python运算符对应的魔术方法 1>.比较运算符 <: ...
- Pthon魔术方法(Magic Methods)-bool
Pthon魔术方法(Magic Methods)-bool 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.bool方法 __bool__: 内建函数bool(),或者对象放在逻 ...
- Pthon魔术方法(Magic Methods)-hash
Pthon魔术方法(Magic Methods)-hash 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.hash方法 __hash__: 内建函数hash()调用的返回值,返 ...
- Pthon魔术方法(Magic Methods)-可视化
Pthon魔术方法(Magic Methods)-可视化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.关于可视化的魔术方法简介 __str__: str()函数,format ...
随机推荐
- iOS - 截取数组前几个元素放入新的数组,剩余的放入另外一个数组
NSArray *array = [NSArray arrayWithObjects:@"Crystal",@"Maisie",@"Lukas&quo ...
- DELPHI 数据库控件心得
TField对象的SetText和GetText事件处理函数 使用TField对象的SetText和GetText事件处理函数可方便的解决字段的代码与代码所对应值的显示问题 TSimpleDatase ...
- phpspreadsheet 中文文档(二) 结构+自动筛选
2019年10月11日13:55:41 原理图 自动加载器 PhpSpreadsheet依赖于Composer自动加载器.因此,在独立使用PhpSpreadsheet之前,请确保先运行composer ...
- [ ceph ] CEPH 部署完整版(CentOS 7 + luminous)
1. 前言 拜读了 胖哥的(el7+jewel)完整部署 受益匪浅,目前 CEPH 已经更新到 M 版本,配置方面或多或少都有了变动,本博文就做一个 ceph luminous 版本完整的配置安装. ...
- ubuntu16.04+cuda8.0+cudnn6.0安装mxnet(极简!+成功!)
安装MXNet 1.安装 CUDA8.0对应的mxnet版本是mxnet-cu80(同理如果是CUDA9.0对应版本则是mxnet-cu90). 如果pip安装过慢,请参考 Ubuntu16.10下配 ...
- sql简单存储过程分享
很多程序员朋友都视sql为洪湖水猛兽,其实深入分析一下,多用些时间与耐心,sql还是可以理解的. 本文主要是针对刚刚接触sql的新手朋友,进行一个sql存储过程的简单分享. 小子第一次发布文章,也是借 ...
- CentOS7-Docker 配置国内镜像源
Docker中国官方镜像加速 --registry-mirror=https://registry.docker-cn.com 网易163镜像加速 --registry-mirror=http://h ...
- 005 SpringCloud 学习笔记01-----系统架构的演变
1.系统架构的演变 随着互联网的发展,网站应用的规模不断扩大.需求的激增,带来的是技术上的压力.系统架构也因此不断的演进.升级.迭代.从单一应用,到垂直拆分,到分布式服务,到SOA,以及现在火热的微服 ...
- 19 SpringMVC 拦截器
1. 拦截器的概述(1)SpringMVC框架中的拦截器用于对处理器进行预处理和后处理的技术.(2)可以定义拦截器链,连接器链就是将拦截器按着一定的顺序结成一条链,在访问被拦截的方法时,拦截器链 中的 ...
- 备份和还原 第三篇:master 数据库的备份和还原
在SQL Server 中,master 数据库记录系统级别的元数据,例如,logon accounts, endpoints, linked servers, and system configur ...