国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python通過函數名調用函數的幾種場景

瀏覽:5日期:2022-07-10 11:23:27

一、說明

之前寫了一篇“Python執行系統命令教程”講了如何執行系統命令。

除了執行系統命令外,我們有時還需要動態地執行一些python代碼,有經驗的朋友就會知道可以使用內置函數eval實現這一需求,如eval('print(__file__)'),這還是比較簡單的。

但如果要動態執行一個函數,講的資料就會少一點,這次就要看這個需求該如何實現。

二、通過eval實現

2.1 通過eval調用同一個類內的函數

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'] # 就直接調用。如果有其他參數,一樣地傳就好了 # 另外也可以是'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 通過eval調用同一個文件內的一級函數

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'] # 就直接調用。如果有其他參數,一樣地傳就好了 # 另外也可以是'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()

三、通過getattr實現

3.1 通過函數名調用同一個類內的函數

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']) # 就直接調用。如果有其他參數,一樣地傳就好了 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 通過函數名調用其他類的函數

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傳被調用的函數所在的類的類實例 testb_obj = TestB() be_called_function = getattr(testb_obj, self.config_dict['be_called_function_name']) # 就直接調用。如果有其他參數,一樣地傳就好了 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 通過函數名調用同文件的一級函數

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傳當前模塊名 module_name = sys.modules[’__main__’] be_called_function = getattr(module_name, self.config_dict['be_called_function_name']) # 就直接調用。如果有其他參數,一樣地傳就好了 be_called_function() passdef be_called_function(): print('here is be_called_function.')if __name__ == '__main__': obj = TestA() obj.active_call_function()

3.4 通過函數名調用在其他文件的一級函數

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傳函數所在模塊名 # __import__()傳函數所在文件 module_name = __import__('test_call_function_by_string1') be_called_function = getattr(module_name, self.config_dict['be_called_function_name']) # 就直接調用。如果有其他參數,一樣地傳就好了 be_called_function() passif __name__ == '__main__': obj = TestA() obj.active_call_function()

以上就是python通過函數名調用函數的幾種場景的詳細內容,更多關于python通過函數名調用函數的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 兖州市| 桐庐县| 阿尔山市| 临夏县| 汕尾市| 马山县| 綦江县| 卢氏县| 宁城县| 安康市| 平安县| 澳门| 洛隆县| 清苑县| 南雄市| 长乐市| 当雄县| 翼城县| 清水县| 北川| 玉门市| 万山特区| 海原县| 祁连县| 保康县| 延吉市| 巫溪县| 浙江省| 太仆寺旗| 旬阳县| 永清县| 行唐县| 利辛县| 安义县| 梧州市| 元江| 漯河市| 江城| 祥云县| 东平县| 石屏县|