您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)python反射機(jī)制是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
反射
反射機(jī)制就是在運(yùn)行時(shí),動(dòng)態(tài)的確定對(duì)象的類(lèi)型,并可以通過(guò)字符串調(diào)用對(duì)象屬性、方法、導(dǎo)入模塊,是一種基于字符串的事件驅(qū)動(dòng)。
解釋型語(yǔ)言:程序不需要編譯,程序在運(yùn)行時(shí)才翻譯成機(jī)器語(yǔ)言,每執(zhí)行一次都要翻譯一次。因此效率比較低。相對(duì)于編譯型語(yǔ)言存在 的,源代碼不是直接翻譯成機(jī)器語(yǔ)言,而是先翻譯成中間代碼,再由解釋器對(duì)中間代碼進(jìn)行解釋運(yùn)行。 比如Python/JavaScript / Perl /Shell等都是解釋型語(yǔ)言。
python是一門(mén)解釋型語(yǔ)言,因此對(duì)于反射機(jī)制支持很好。在python中支持反射機(jī)制的函數(shù)有g(shù)etattr()、setattr()、delattr()、exec()、eval()、__import__,這些函數(shù)都可以執(zhí)行字符串。
eval
計(jì)算指定表達(dá)式的值。它只能執(zhí)行單個(gè)表達(dá)式,而不能是復(fù)雜的代碼邏輯。而且不能是賦值表達(dá)式。
單個(gè)表達(dá)式
a = "12 + 43"b = eval(a) print(b)
復(fù)雜表達(dá)式
a = "print(12 + 43); print(1111)"b = eval(a) print(b)# 輸出:Traceback (most recent call last): File "xxxx.py", line 10, in <module> b = eval(a) File "<string>", line 1 print(12 + 43); print(1111) ^ SyntaxError: invalid syntax
賦值
a = 1b = eval("a = 21") print(b)
通常我們使用eval的時(shí)候,主要是使用它的返回值,獲取表達(dá)式計(jì)算出的值
exec
執(zhí)行復(fù)雜表達(dá)式,返回值永遠(yuǎn)都是None
b = exec("aa = 21") print(b) # None,exec返回值為Noneprint(aa) # 21,exec執(zhí)行了賦值語(yǔ)句,并定義了aa變量
執(zhí)行復(fù)雜語(yǔ)句
a = '''ret = [] for i in range(10): ret.append(i)'''exec(a) print(ret) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
導(dǎo)入模塊
# 導(dǎo)入模塊exec("import config") print(config.KEYWORD)# 動(dòng)態(tài)創(chuàng)建類(lèi)class Base: def __init__(self): print("Base") a = "Base"exec(a+"()")
導(dǎo)入模塊這個(gè)功能就非常屌了,這樣我們就可以動(dòng)態(tài)的創(chuàng)建各種模塊類(lèi)。
eval()函數(shù)和exec()函數(shù)的區(qū)別: eval()函數(shù)只能計(jì)算單個(gè)表達(dá)式的值,而exec()函數(shù)可以動(dòng)態(tài)運(yùn)行代碼段。 eval()函數(shù)可以有返回值,而exec()函數(shù)返回值永遠(yuǎn)為None。
看一下這個(gè)例子
class Base: def __init__(self): print("Base") def test(self): print("test") return "Base::test"
如果我們想通過(guò)字符串來(lái)調(diào)用a對(duì)象的test方法,應(yīng)該怎么做呢,如果要獲取返回值,那么可以使用
b = eval("a.test()") print(b)
輸出:
test Base::test
如果不需要獲取返回值,那么可以使用exec,exec("a.test()"),輸出:test
雖然我們可以使用eval和exec來(lái)執(zhí)行以上代碼,但是這種方式有一個(gè)缺陷,假如這個(gè)屬性是不存在的,那么這種調(diào)用就會(huì)報(bào)錯(cuò)。那么做好的方式是什么呢?先判斷屬性是否存在,如果存在就調(diào)用,不存在就不調(diào)用,python為我們提供了一套方法:hasattr、getattr、setattr、delattr
hasattr
def hasattr(*args, **kwargs): # real signature unknown """ Return whether the object has an attribute with the given name. This is done by calling getattr(obj, name) and catching AttributeError. """ pass
通過(guò)源碼注釋我們知道,它返回對(duì)象是否具有指定名稱(chēng)的屬性。而且它是通過(guò)調(diào)用getattr并捕獲AttributeError異常來(lái)判斷的。就像上面的屬性調(diào)用,我們就可以使用hasattr(a, "test")來(lái)判斷,通過(guò)源碼注釋我們也可以思考一下,eval這種是不是也可以實(shí)現(xiàn)這種方法呢?
def has_attr(obj, name): try: eval("obj.%s()" % name) return True except AttributeError as e: return Falsea = Base()if has_attr(a, "test"): eval("a.test()")# 輸出:Base test test
但是這種方式是有缺陷的,因?yàn)閠est輸出了兩次,因?yàn)槲覀冋{(diào)用了兩次test(),這跟我們想要的效果不一樣。如果用hasattr呢,這個(gè)函數(shù)就不會(huì)在判斷的時(shí)候調(diào)用一次了。
getattr()
有了判斷屬性是否存在的函數(shù),那么就得有獲取屬性的函數(shù)了
def getattr(object, name, default=None): # known special case of getattr """ 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
從源碼注釋我們就能知道獲取object對(duì)象的名為name的屬性,想到與object.name,如果提供了default參數(shù),那么當(dāng)屬性不存在的時(shí)候,就會(huì)返回默認(rèn)值。同樣是上面的例子:
a = Base()if hasattr(a, "test"): func = getattr(a, "test") func()# 輸出:Base test
從例子中我們可以看出,hasattr并沒(méi)有調(diào)用test函數(shù),而且getattr獲取到的是函數(shù)對(duì)象,也沒(méi)有調(diào)用它,通過(guò)我們主動(dòng)執(zhí)行func()才執(zhí)行了a.test()函數(shù),這樣相比于exec和eval就靈活了許多。
setattr
判斷和獲取屬性有了,那么設(shè)置屬性也是需要的
def setattr(x, y, v): # real signature unknown; restored from __doc__ """ Sets the named attribute on the given object to the specified value. setattr(x, 'y', v) is equivalent to ``x.y = v'' """ pass
將一個(gè)特殊值設(shè)置給object對(duì)象的name屬性,相當(dāng)于x.y = v
class Base: def __init__(self): self.name = "name"a = Base() setattr(a, "name", "zhangsan") print(a.name) # 改變?cè)袑傩缘闹祍etattr(a, "age", 32) print(getattr(a, "age")) # 新增不存在的屬性,并設(shè)置值
雖然setattr(a, "age", 32)等于a.age=32,但是我們不要忘了,這是通過(guò)一個(gè)字符串來(lái)增加的屬性。
判斷、獲取、增加都有了,當(dāng)然還有刪除delattr,這個(gè)我們就不詳述了,接下來(lái)我們要看一個(gè)比較重要的方法。
import
在學(xué)習(xí)exec的時(shí)候,我們有一個(gè)例子,導(dǎo)入配置文件exec("import config"),針對(duì)這種方式python也為我們提供了更好的方法。
def __import__(name, globals=None, locals=None, fromlist=(), level=0): # real signature unknown; restored from __doc__ """ __import__(name, globals=None, locals=None, fromlist=(), level=0) -> module Import a module. Because this function is meant for use by the Python interpreter and not for general use, it is better to use importlib.import_module() to programmatically import a module. The globals argument is only used to determine the context; they are not modified. The locals argument is unused. The fromlist should be a list of names to emulate ``from name import ...'', or an empty list to emulate ``import name''. When importing a module from a package, note that __import__('A.B', ...) returns package A when fromlist is empty, but its submodule B when fromlist is not empty. The level argument is used to determine whether to perform absolute or relative imports: 0 is absolute, while a positive number is the number of parent directories to search relative to the current module. """ pass
在這里我們最需要關(guān)注的是formlist參數(shù),先看一個(gè)簡(jiǎn)單的例子:
a = __import__("config") print(a.KEYWORD)
config是一個(gè)py腳本-config.py,內(nèi)部有一個(gè)變量KEYWORD,我們要通過(guò)其他py模塊來(lái)導(dǎo)入這個(gè)文件,使用__import__我們就可以把它導(dǎo)入為一個(gè)對(duì)象,然后使用對(duì)象的方式去調(diào)用,而不是一直用exec字符串的形式去調(diào)用。上面我們說(shuō)了formlist這個(gè)參數(shù)需要關(guān)注,為什么呢?我們新增了一個(gè)模塊:comm。模塊內(nèi)有一個(gè)腳本function.py
# function.pydef comm_function(): print("test_module")
我們現(xiàn)在想通過(guò)動(dòng)態(tài)引入的方式調(diào)用comm_function函數(shù),那么按照上面的方式來(lái)
a = __import__("comm.function") a.comm_function()
結(jié)果輸出:
Traceback (most recent call last): File "xxx.py", line 10, in <module> print(a.comm_function()) AttributeError: module 'comm' has no attribute 'comm_function'
意思是comm模塊沒(méi)有comm_function這個(gè)屬性,為什么是comm模塊而不是function呢?我們可以打印一下模塊的引入名稱(chēng)print(a.__name__),打印的結(jié)果是comm,就是說(shuō)我們通過(guò)上面的方式只是引入comm,而不是function。其實(shí)通過(guò)源碼注釋我們就知道了,__import__(A.B),如果fromlist為空,返回的是A包,如果不為空,則返回其子包B。修改一下我們的代碼:
a = __import__("comm.function", fromlist=True) print(a.__name__) a.comm_function()# 輸出:comm.function test_module
引入的模塊和執(zhí)行函數(shù)都正確了,符合了我們的預(yù)期要求。
總結(jié)
通過(guò)以上的函數(shù)學(xué)習(xí),其中有常用的,也有不常用的,但是這些函數(shù)在我們進(jìn)行框架設(shè)計(jì)時(shí)是必不可少的,尤其是__import__,接下來(lái)我們還會(huì)繼續(xù)看框架設(shè)計(jì)中最重要的一個(gè)概念--元編程。學(xué)完了這些概念就可以設(shè)計(jì)框架了。開(kāi)玩笑的,哪有那么簡(jiǎn)單。閱讀源碼是一種增長(zhǎng)知識(shí)的最快捷方式,但是前提是基礎(chǔ)一定要打好。否則看源碼是一頭霧水。我們整理完這些概念后,在找?guī)讉€(gè)源碼庫(kù)看看,學(xué)習(xí)一下里面的設(shè)計(jì)理念。
關(guān)于python反射機(jī)制是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。