python通過(guò)函數(shù)名調(diào)用函數(shù)的幾種場(chǎng)景
一、說(shuō)明
之前寫了一篇“Python執(zhí)行系統(tǒng)命令教程”講了如何執(zhí)行系統(tǒng)命令。
除了執(zhí)行系統(tǒng)命令外,我們有時(shí)還需要?jiǎng)討B(tài)地執(zhí)行一些python代碼,有經(jīng)驗(yàn)的朋友就會(huì)知道可以使用內(nèi)置函數(shù)eval實(shí)現(xiàn)這一需求,如eval('print(__file__)'),這還是比較簡(jiǎn)單的。
但如果要?jiǎng)討B(tài)執(zhí)行一個(gè)函數(shù),講的資料就會(huì)少一點(diǎn),這次就要看這個(gè)需求該如何實(shí)現(xiàn)。
二、通過(guò)eval實(shí)現(xiàn)
2.1 通過(guò)eval調(diào)用同一個(gè)類內(nèi)的函數(shù)
class TestA: def __init__(self): self.config_dict = { 'be_called_function_name': 'self.be_called_function()', } pass def active_call_function(self): print('here is active_call_function.') be_called_function_name = self.config_dict['be_called_function_name'] # 就直接調(diào)用。如果有其他參數(shù),一樣地傳就好了 # 另外也可以是'be_called_function_name'是'be_called_function',然后eval(be_called_function_name)() eval(be_called_function_name) pass def be_called_function(self): print('here is be_called_function.')if __name__ == '__main__': obj = TestA() obj.active_call_function()
2.2 通過(guò)eval調(diào)用同一個(gè)文件內(nèi)的一級(jí)函數(shù)
class TestA: def __init__(self): self.config_dict = { 'be_called_function_name': 'be_called_function()', } pass def active_call_function(self): print('here is active_call_function.') be_called_function_name = self.config_dict['be_called_function_name'] # 就直接調(diào)用。如果有其他參數(shù),一樣地傳就好了 # 另外也可以是'be_called_function_name'是'be_called_function',然后eval(be_called_function_name)() eval(be_called_function_name) passdef be_called_function(): print('here is be_called_function.')if __name__ == '__main__': obj = TestA() obj.active_call_function()
三、通過(guò)getattr實(shí)現(xiàn)
3.1 通過(guò)函數(shù)名調(diào)用同一個(gè)類內(nèi)的函數(shù)
class TestA: def __init__(self): self.config_dict = { 'be_called_function_name': 'be_called_function', } pass def active_call_function(self): print('here is active_call_function.') # getaattr(module_name, function_name),module_name傳self即可 be_called_function = getattr(self, self.config_dict['be_called_function_name']) # 就直接調(diào)用。如果有其他參數(shù),一樣地傳就好了 be_called_function() pass def be_called_function(self): print('here is be_called_function.')if __name__ == '__main__': obj = TestA() obj.active_call_function()
3.2 通過(guò)函數(shù)名調(diào)用其他類的函數(shù)
class TestA: def __init__(self): self.config_dict = { 'be_called_function_name': 'be_called_function', } pass def active_call_function(self): print('here is active_call_function.') # getaattr(module_name, function_name),module_name傳被調(diào)用的函數(shù)所在的類的類實(shí)例 testb_obj = TestB() be_called_function = getattr(testb_obj, self.config_dict['be_called_function_name']) # 就直接調(diào)用。如果有其他參數(shù),一樣地傳就好了 be_called_function() passclass TestB: def be_called_function(self): print('here is be_called_function.')if __name__ == '__main__': obj = TestA() obj.active_call_function()
3.3 通過(guò)函數(shù)名調(diào)用同文件的一級(jí)函數(shù)
import sysclass TestA: def __init__(self): self.config_dict = { 'be_called_function_name': 'be_called_function', } pass def active_call_function(self): print('here is active_call_function.') # getaattr(module_name, function_name),module_name傳當(dāng)前模塊名 module_name = sys.modules[’__main__’] be_called_function = getattr(module_name, self.config_dict['be_called_function_name']) # 就直接調(diào)用。如果有其他參數(shù),一樣地傳就好了 be_called_function() passdef be_called_function(): print('here is be_called_function.')if __name__ == '__main__': obj = TestA() obj.active_call_function()
3.4 通過(guò)函數(shù)名調(diào)用在其他文件的一級(jí)函數(shù)
class TestA: def __init__(self): self.config_dict = { 'be_called_function_name': 'be_called_function', } pass def active_call_function(self): print('here is active_call_function.') # getaattr(module_name, function_name),module_name傳函數(shù)所在模塊名 # __import__()傳函數(shù)所在文件 module_name = __import__('test_call_function_by_string1') be_called_function = getattr(module_name, self.config_dict['be_called_function_name']) # 就直接調(diào)用。如果有其他參數(shù),一樣地傳就好了 be_called_function() passif __name__ == '__main__': obj = TestA() obj.active_call_function()
以上就是python通過(guò)函數(shù)名調(diào)用函數(shù)的幾種場(chǎng)景的詳細(xì)內(nèi)容,更多關(guān)于python通過(guò)函數(shù)名調(diào)用函數(shù)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
