一.反射
反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(自省)。这一概念的提出很快引发了计算机科学领域关于应用反射性的研究。它首先被程序语言的设计领域所采用,并在Lisp和面向对象方面取得了成绩。
python面向对象中的反射:通过字符串的形式操作对象相关的属性。python中的一切事物都是对象(都可以使用反射)
四个可以实现自省的函数
下列方法适用于类和对象(一切皆对象,类本身也是一个对象)
1.1 对象的反射
class Foo: f = '类的静态变量' def __init__(self,name,age): self.name=name self.age=age
<span class="token keyword">def</span> <span class="token function">say_hi</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'hi,%s'</span><span class="token operator">%</span>self<span class="token punctuation">.</span>name<span class="token punctuation">)</span>
obj=Foo('egon',73)
#检测是否含有某属性
print(hasattr(obj,'name'))
print(hasattr(obj,'say_hi'))#获取属性
n=getattr(obj,'name')
print(n)
func=getattr(obj,'say_hi')
func()print(getattr(obj,'aaaaaaaa','不存在啊')) #报错
#设置属性
setattr(obj,'sb',True)
setattr(obj,'show_name',lambda self:self.name+'sb')
print(obj.dict)
print(obj.show_name(obj))#删除属性
delattr(obj,'age')
delattr(obj,'show_name')
delattr(obj,'show_name111')#不存在,则报错print(obj.dict)
对实例化对象的示例
1.2 对类的反射
class Foo(object):
staticField <span class="token operator">=</span> <span class="token string">"old boy"</span> <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">'wupeiqi'</span> <span class="token keyword">def</span> <span class="token function">func</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token string">'func'</span> <span class="token decorator annotation punctuation">@staticmethod</span> <span class="token keyword">def</span> <span class="token function">bar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token string">'bar'</span>
print getattr(Foo, 'staticField')
print getattr(Foo, 'func')
print getattr(Foo, 'bar')
1.3 当前模块的反射
import sys
def s1():
print 's1'def s2():
print 's2'this_module = sys.modules[name]
hasattr(this_module, 's1')
getattr(this_module, 's2')
1.4 其他模块的反射
#一个模块中的代码 def test(): print('from the test') """ 程序目录: module_test.py index.py
当前文件:
index.py
"""
# 另一个模块中的代码
import module_test as obj#obj.test()
print(hasattr(obj,'test'))
getattr(obj,'test')()
1.5 反射的应用
了解了反射的四个函数。那么反射到底有什么用呢?它的应用场景是什么呢?
现在让我们打开浏览器,访问一个网站,你单击登录就跳转到登录界面,你单击注册就跳转到注册界面,等等,其实你单击的其实是一个个的链接,每一个链接都会有一个函数或者方法来处理。
没学反射之前的解决方式
class User: def login(self): print('欢迎来到登录页面')
<span class="token keyword">def</span> <span class="token function">register</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'欢迎来到注册页面'</span><span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">save</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'欢迎来到存储页面'</span><span class="token punctuation">)</span>
while 1:
choose = input('>>>').strip()
if choose == 'login':
obj = User()
obj.login()<span class="token keyword">elif</span> choose <span class="token operator">==</span> <span class="token string">'register'</span><span class="token punctuation">:</span> obj <span class="token operator">=</span> User<span class="token punctuation">(</span><span class="token punctuation">)</span> obj<span class="token punctuation">.</span>register<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">elif</span> choose <span class="token operator">==</span> <span class="token string">'save'</span><span class="token punctuation">:</span> obj <span class="token operator">=</span> User<span class="token punctuation">(</span><span class="token punctuation">)</span> obj<span class="token punctuation">.</span>save<span class="token punctuation">(</span><span class="token punctuation">)</span>
学了反射之后解决方式
class User: def login(self): print('欢迎来到登录页面')
<span class="token keyword">def</span> <span class="token function">register</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'欢迎来到注册页面'</span><span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">save</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'欢迎来到存储页面'</span><span class="token punctuation">)</span>
user = User()
while 1:
choose = input('>>>').strip()
if hasattr(user,choose):
func = getattr(user,choose)
func()
else:
print('输入错误。。。。')
这样就可以明确的感觉到反射的好处,我们一直使用的字典方式其实就是反射的思想
只不过反射比我们之前写法方法还有霸道