什么是反射
反射的概念是由Smith在1982年首次提出的杭隙,主要是指程序可以訪問专肪、檢測(cè)和修改它本身狀態(tài)或行為的一種能力(自市弊觥)兑凿。這一概念的提出很快引發(fā)了計(jì)算機(jī)科學(xué)領(lǐng)域關(guān)于應(yīng)用反射性的研究疤祭。它首先被程序語言的設(shè)計(jì)領(lǐng)域所采用,并在Lisp和面向?qū)ο蠓矫嫒〉昧顺煽?jī)盼产。
python面向?qū)ο笾械姆瓷?b>:通過字符串的形式操作對(duì)象相關(guān)的屬性。python中的一切事物都是對(duì)象(都可以使用反射)
四個(gè)可以實(shí)現(xiàn)自省的函數(shù)
下列方法適用于類和對(duì)象(一切皆對(duì)象勺馆,類本身也是一個(gè)對(duì)象)
def hasattr(*args, **kwargs):
? ? """
? ? Return whether the object has an attribute with the given name.
? ? This is done by calling getattr(obj, name) and catching AttributeError.
? ? """
? ? pass
def getattr(object, name, default=None):
? ? """
? ? getattr(object, name[, default]) -> value
? ? Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
? ? When a default argument is given, it is returned when the attribute doesn't
? ? exist; without it, an exception is raised in that case.
? ? """
? ? pass
def setattr(x, y, v):
? ? """
? ? Sets the named attribute on the given object to the specified value.
? ? setattr(x, 'y', v) is equivalent to ``x.y = v''
? ? """
? ? pass
def delattr(x, y):
? ? """
? ? Deletes the named attribute from the given object.
? ? delattr(x, 'y') is equivalent to ``del x.y''
? ? """
? ? pass
用法舉例
class Person(object):
? ? def __init__(self,name,age):
? ? ? ? self.name = name
? ? ? ? self.age = age
? ? def say_hi(self):
? ? ? ? print("hi,guys , my name is " ,self.name)
obj=Person('Alex',26)
#檢測(cè)是否含有某屬性
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','不存在啊')) #報(bào)錯(cuò)
#設(shè)置屬性
setattr(obj,'hobbie',"girl")
setattr(obj,'show_name',lambda self:self.name+'--%s' % self.age)
print(obj.__dict__)
print(obj.show_name(obj))
#刪除屬性
delattr(obj,'age')
delattr(obj,'show_name')
#delattr(obj,'show_name111') # 不存在,則報(bào)錯(cuò)
print(obj.__dict__)
反射模塊成員
除了可以用來檢測(cè)類中有沒有某個(gè)方法戏售,還可以用來檢測(cè)模塊下有沒有方法侨核、類、或者變量
def s1():
? ? print('s1')
class Person(object):
? ? def __init__(self,name,age):
? ? ? ? self.name = name
? ? ? ? self.age = age
name = "test"
this_module = sys.modules[__name__] # __name__ 會(huì)動(dòng)態(tài)的代表當(dāng)前模塊名
print(hasattr(this_module, 's1'))
print(hasattr(this_module, 'name'))
print(getattr(this_module, 'Person'))
p = getattr(this_module, 'Person')
p("Alex",22)
反射從其它模塊導(dǎo)入的方法
反射的應(yīng)用
了解了反射的四個(gè)函數(shù)灌灾。那么反射到底有什么用呢搓译?它的應(yīng)用場(chǎng)景是什么呢?
現(xiàn)在讓我們打開瀏覽器锋喜,訪問一個(gè)網(wǎng)站侥衬,你單擊登錄就跳轉(zhuǎn)到登錄界面,你單擊注冊(cè)就跳轉(zhuǎn)到注冊(cè)界面跑芳,等等轴总,其實(shí)你單擊的其實(shí)是一個(gè)個(gè)的鏈接,每一個(gè)鏈接都會(huì)有一個(gè)函數(shù)或者方法來處理博个。
沒學(xué)反射之前的解決方式
class User:
? ? def login(self):
? ? ? ? print('歡迎來到登錄頁面')
? ? def register(self):
? ? ? ? print('歡迎來到注冊(cè)頁面')
? ? def save(self):
? ? ? ? print('歡迎來到存儲(chǔ)頁面')
while 1:
? ? choose = input('>>>').strip()
? ? if choose == 'login':
? ? ? ? obj = User()
? ? ? ? obj.login()
? ? elif choose == 'register':
? ? ? ? obj = User()
? ? ? ? obj.register()
? ? elif choose == 'save':
? ? ? ? obj = User()
? ? ? ? obj.save()
學(xué)了反射之后解決方式
class User:
? ? def login(self):
? ? ? ? print('歡迎來到登錄頁面')
? ? def register(self):
? ? ? ? print('歡迎來到注冊(cè)頁面')
? ? def save(self):
? ? ? ? print('歡迎來到存儲(chǔ)頁面')
user = User()
while 1:
? ? choose = input('>>>').strip()
? ? if hasattr(user,choose):
? ? ? ? func = getattr(user,choose)
? ? ? ? func()
? ? else:
? ? ? ? print('輸入錯(cuò)誤怀樟。。盆佣。往堡。')
這樣就可以明確的感覺到反射的好處
如果在程序去過過程中,要求程序按用戶輸入的指令字符串導(dǎo)入對(duì)應(yīng)的模塊共耍,在只知道一個(gè)模塊的字符串名字的情況下虑灰,能否導(dǎo)入?
import importlib
__import__('import_lib.metaclass') #這是解釋器自己內(nèi)部用的
#importlib.import_module('import_lib.metaclass') #與上面這句效果一樣痹兜,官方建議用這個(gè)