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 ...
随机推荐
- laravel5.8ajax请求auth认证返回302的解决方法。
注册 /app/Http/Controller/Auth/RegisterController.php <?php namespace App\Http\Controllers\Auth; us ...
- 最常见的Java面试题及答案汇总(四)
反射 57. 什么是反射? 反射主要是指程序可以访问.检测和修改它本身状态或行为的一种能力 Java反射: 在Java运行时环境中,对于任意一个类,能否知道这个类有哪些属性和方法?对于任意一个对象,能 ...
- Linux下配置Golang开发环境
前几天无意间看到了微信推送的golang开发的消息,看到golang那么牛逼,突然心血来潮想学习一下go.工欲善其事必先利其器,想做go开发,必须先配置好go的开发环境(就像开发Java先安装配置jd ...
- python中__init__.py的作用、module和package
控制包的导入行为: 1.声明当前文件是一个可导入的包: 2.如果当下包下有多个.py文件使用__ all__ = [ '模块名'],也就是form XXX import YYY module和pack ...
- 腾讯物联网操作系统正式开源,最小体积仅1.8 KB
9月18日,腾讯宣布将开源自主研发的轻量级物联网实时操作系统TencentOS tiny.相比市场上其它系统,腾讯TencentOS tiny在资源占用.设备成本.功耗管理以及安全稳定等层面极具竞争力 ...
- Content-type"是"application/json的作用
request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody ...
- 处理登录时,AJAX的状态码无权限情况
$.ajaxSetup({ complete: function(XMLHttpRequest, textStatus) { }, error:function(jqXHR,textStatus,er ...
- day21——面向对象初识、结构、从类名研究类、从对象研究类、logging模块进阶版
day21 面向对象的初识 面向对象第一个优点: 对相似功能的函数,同一个业务下的函数进行归类,分类. 想要学习面向对象必须站在一个上帝的角度去分析考虑问题. 类: 具有相同属性和功能的一类事物. 对 ...
- Python之路【第二十三篇】:数据库基础
数据库的简介 数据库 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展性, ...
- datanode启动异常(Incompatible clusterIDs)
问题: 正常start-all.sh无法启动datanode进程,但是./hadoop-daemon.sh start datanode又可以启动.过一会后datanode进程又莫名消失. 原理: 多 ...