python的反射機制是怎樣的

python的反射機制是怎樣的,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比松嶺網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式松嶺網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋松嶺地區(qū)。費用合理售后完善,10年實體公司更值得信賴。

反射

反射機制就是在運行時,動態(tài)的確定對象的類型,并可以通過字符串調(diào)用對象屬性、方法、導入模塊,是一種基于字符串的事件驅(qū)動。

解釋型語言:程序不需要編譯,程序在運行時才翻譯成機器語言,每執(zhí)行一次都要翻譯一次。因此效率比較低。相對于編譯型語言存在的,源代碼不是直接翻譯成機器語言,而是先翻譯成中間代碼,再由解釋器對中間代碼進行解釋運行。比如Python/JavaScript / Perl /Shell等都是解釋型語言。

python是一門解釋型語言,因此對于反射機制支持很好。在python中支持反射機制的函數(shù)有g(shù)etattr()、setattr()、delattr()、exec()、eval()、__import__,這些函數(shù)都可以執(zhí)行字符串。

eval

計算指定表達式的值。它只能執(zhí)行單個表達式,而不能是復雜的代碼邏輯。而且不能是賦值表達式。

單個表達式:

a = "12 + 43"
b = eval(a)
print(b)

復雜表達式:

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 = 1
b = eval("a = 21")
print(b)

通常我們使用eval的時候,主要是使用它的返回值,獲取表達式計算出的值

exec

執(zhí)行復雜表達式,返回值永遠都是None

b = exec("aa = 21")
print(b) # None,exec返回值為None
print(aa) # 21,exec執(zhí)行了賦值語句,并定義了aa變量

執(zhí)行復雜語句:

a = '''ret = []
for i in range(10):
 ret.append(i)'''
exec(a)
print(ret) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

導入模塊:

# 導入模塊
exec("import config")
print(config.KEYWORD)
# 動態(tài)創(chuàng)建類
class Base:
 def __init__(self):
 print("Base")
a = "Base"
exec(a+"()")

導入模塊這個功能就非常厲害了,這樣我們就可以動態(tài)的創(chuàng)建各種模塊類。

eval()函數(shù)和exec()函數(shù)的區(qū)別:

eval()函數(shù)只能計算單個表達式的值,而exec()函數(shù)可以動態(tài)運行代碼段。

eval()函數(shù)可以有返回值,而exec()函數(shù)返回值永遠為None。

再看一下下面的例子:

class Base:
 def __init__(self):
 print("Base")
 def test(self):
 print("test")
 return "Base::test"

如果我們想通過字符串來調(diào)用a對象的test方法,應(yīng)該怎么做呢,如果要獲取返回值,那么可以使用

b = eval("a.test()")
print(b)

輸出:

test

Base::test

如果不需要獲取返回值,那么可以使用exec,exec("a.test()"),輸出:test

雖然我們可以使用eval和exec來執(zhí)行以上代碼,但是這種方式有一個缺陷,假如這個屬性是不存在的,那么這種調(diào)用就會報錯。那么做好的方式是什么呢?先判斷屬性是否存在,如果存在就調(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

通過源碼注釋我們知道,它返回對象是否具有指定名稱的屬性。而且它是通過調(diào)用getattr并捕獲AttributeError異常來判斷的。就像上面的屬性調(diào)用,我們就可以使用hasattr(a, "test")來判斷,通過源碼注釋我們也可以思考一下,eval這種是不是也可以實現(xiàn)這種方法呢?

def has_attr(obj, name):
 try:
 eval("obj.%s()" % name)
 return True
 except AttributeError as e:
 return False
a = Base()
if has_attr(a, "test"):
 eval("a.test()")
# 輸出:
Base
test
test

但是這種方式是有缺陷的,因為test輸出了兩次,因為我們調(diào)用了兩次test(),這跟我們想要的效果不一樣。如果用hasattr呢,這個函數(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對象的名為name的屬性,想到與object.name,如果提供了default參數(shù),那么當屬性不存在的時候,就會返回默認值。同樣是上面的例子:

a = Base()
if hasattr(a, "test"):
 func = getattr(a, "test")
 func()
# 輸出:
Base
test

從例子中我們可以看出,hasattr并沒有調(diào)用test函數(shù),而且getattr獲取到的是函數(shù)對象,也沒有調(diào)用它,通過我們主動執(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

將一個特殊值設(shè)置給object對象的name屬性,相當于x.y = v

class Base:
 def __init__(self):
 self.name = "name"
a = Base()
setattr(a, "name", "zhangsan")
print(a.name) # 改變原有屬性的值
setattr(a, "age", 32)
print(getattr(a, "age")) # 新增不存在的屬性,并設(shè)置值

雖然setattr(a, "age", 32)等于a.age=32,但是我們不要忘了,這是通過一個字符串來增加的屬性。

判斷、獲取、增加都有了,當然還有刪除delattr,這個我們就不詳述了,接下來我們要看一個比較重要的方法。

import

在學習exec的時候,我們有一個例子,導入配置文件exec("import config"),針對這種方式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ù),先看一個簡單的例子:

a = __import__("config")
print(a.KEYWORD)

config是一個py腳本-config.py,內(nèi)部有一個變量KEYWORD,我們要通過其他py模塊來導入這個文件,使用__import__我們就可以把它導入為一個對象,然后使用對象的方式去調(diào)用,而不是一直用exec字符串的形式去調(diào)用。上面我們說了formlist這個參數(shù)需要關(guān)注,為什么呢?我們新增了一個模塊:comm。模塊內(nèi)有一個腳本function.py

# function.py
def comm_function():
 print("test_module")

我們現(xiàn)在想通過動態(tài)引入的方式調(diào)用comm_function函數(shù),那么按照上面的方式來

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模塊沒有comm_function這個屬性,為什么是comm模塊而不是function呢?我們可以打印一下模塊的引入名稱print(a.__name__),打印的結(jié)果是comm,就是說我們通過上面的方式只是引入comm,而不是function。其實通過源碼注釋我們就知道了,__import__(A.B),如果fromlist為空,返回的是A包,如果不為空,則返回其子包B。修改一下我們的代碼:

a = __import__("comm.function", fromlist=True)
print(a.__name__)
a.comm_function()
# 輸出:
comm.function
test_module

引入的模塊和執(zhí)行函數(shù)都正確了,符合了我們的預期要求。

總結(jié)

通過以上的函數(shù)學習,其中有常用的,也有不常用的,但是這些函數(shù)在我們進行框架設(shè)計時是必不可少的,尤其是__import__,接下來我們還會繼續(xù)看框架設(shè)計中最重要的一個概念--元編程。學完了這些概念就可以設(shè)計框架了。開玩笑的,哪有那么簡單。閱讀源碼是一種增長知識的最快捷方式,但是前提是基礎(chǔ)一定要打好。否則看源碼是一頭霧水。我們整理完這些概念后,在找?guī)讉€源碼庫看看,學習一下里面的設(shè)計理念。

關(guān)于python的反射機制是怎樣的問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

當前文章:python的反射機制是怎樣的
URL鏈接:http://muchs.cn/article8/pphgop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)微信小程序、網(wǎng)站排名網(wǎng)站內(nèi)鏈、建站公司、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司