Python類的動態(tài)綁定實現(xiàn)原理
使用實例引用類的屬性時,會發(fā)生動態(tài)綁定。即python會在實例每次引用類屬性時,將對應(yīng)的類屬性綁定到實例上。
動態(tài)綁定的例子:
class A: def test1(self): print('hello') def test2(self): print('world')def bound(): a = A() a.test1() A.test1 = A.test2 a.test1()if __name__ == '__main__': bound()
輸出結(jié)果:
hello2 world
從上述代碼中可以看到,類方法的變化是實時影響實例對方法的調(diào)用的,這說明python是在實例調(diào)用方法的過程中動態(tài)地查找類方法。
動態(tài)綁定的代價:
class A: def test(self): passdef one_loop(limited_time): a = A() for i in range(limited_time): a.test() f = a.test for i in range(limited_time): f()
上圖兩個循環(huán)中,一個調(diào)用a.test(),不斷進行動態(tài)綁定,另一個則先把a.test賦值給f,只有一次動態(tài)綁定,通過對兩個循環(huán)計時,測試動態(tài)綁定的代價。
輸出結(jié)果:
1 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0009999275207519531, 0.008995771408081055, 0.19991111755371094, 1.2715933322906494, 15.831915855407715]2 [0.0, 0.0, 0.0, 0.0, 0.0, 0.12503726671039295, 0.09472344399590288, 0.1999776288967874, 0.131608969147562, 0.1553209370384522]
折線圖中橫坐標(biāo)為log10(循環(huán)次數(shù)),縱坐標(biāo)為秒數(shù)。
輸出數(shù)據(jù)中,第一行為動態(tài)綁定和一次綁定耗費時間的差值,第二行為差值占動態(tài)綁定總時間的比例。
可以看出,在次數(shù)很小的時候,兩者基本沒有差距,或者說差距忽略不計。
在10^7次循環(huán),即千萬次循環(huán)的時候,動態(tài)綁定與靜態(tài)綁定的耗費時間才出現(xiàn)了明顯差異,當(dāng)循環(huán)次數(shù)達到十億級的時候,耗費時間相差15秒之多,約占總時間的15%。
由上可知,動態(tài)綁定效率低于靜態(tài)綁定,但由于綁定代價耗時很少,在次數(shù)很少的時候基本沒有影響。
動態(tài)綁定的優(yōu)點:
class A: def test_hello(self): print('hello')def test_world(self): print('world')def main(): s = A() # 提前綁定 f = s.test_hello # 改變方法 A.test_hello = test_world f() # 動態(tài)綁定 s.test_hello()if __name__ == '__main__': main()
輸出結(jié)果:
hello2 world
類方法的變動能夠?qū)崟r反應(yīng)在動態(tài)綁定上,而提前綁定則無法感知到類方法的變動。
總結(jié):
1. 一次動態(tài)綁定代價很小,當(dāng)綁定次數(shù)少的時候基本不影響效率,當(dāng)綁定次數(shù)達到千萬級時影響才會很顯著。
2. 動態(tài)綁定實時跟蹤類方法的變動,更具靈活性。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章: