python通過函數(shù)名調(diào)用函數(shù)的幾種場(chǎng)景
一、說明
之前寫了一篇“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)。
二、通過eval實(shí)現(xiàn)
2.1 通過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 通過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)
pass
def be_called_function():
print("here is be_called_function.")
if __name__ == "__main__":
obj = TestA()
obj.active_call_function()
三、通過getattr實(shí)現(xiàn)
3.1 通過函數(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 通過函數(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()
pass
class TestB:
def be_called_function(self):
print("here is be_called_function.")
if __name__ == "__main__":
obj = TestA()
obj.active_call_function()
3.3 通過函數(shù)名調(diào)用同文件的一級(jí)函數(shù)
import sys
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傳當(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()
pass
def be_called_function():
print("here is be_called_function.")
if __name__ == "__main__":
obj = TestA()
obj.active_call_function()
3.4 通過函數(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()
pass
if __name__ == "__main__":
obj = TestA()
obj.active_call_function()
以上就是python通過函數(shù)名調(diào)用函數(shù)的幾種場(chǎng)景的詳細(xì)內(nèi)容,更多關(guān)于python通過函數(shù)名調(diào)用函數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python函數(shù)調(diào)用追蹤實(shí)現(xiàn)代碼
- Python根據(jù)字符串調(diào)用函數(shù)過程解析
- Python基于callable函數(shù)檢測(cè)對(duì)象是否可被調(diào)用
- Python函數(shù)遞歸調(diào)用實(shí)現(xiàn)原理實(shí)例解析
- Python基于字典實(shí)現(xiàn)switch case函數(shù)調(diào)用
- python怎么調(diào)用自己的函數(shù)
- Python如何在main中調(diào)用函數(shù)內(nèi)的函數(shù)方式
- 解決python調(diào)用自己文件函數(shù)/執(zhí)行函數(shù)找不到包問題
- python通過函數(shù)名調(diào)用函數(shù)的幾種方法總結(jié)
相關(guān)文章
Python如何實(shí)現(xiàn)守護(hù)進(jìn)程的方法示例
護(hù)進(jìn)程:通常被定義為一個(gè)后臺(tái)進(jìn)程,而且它不屬于任何一個(gè)終端會(huì)話(terminal session)。許多系統(tǒng)服務(wù)由守護(hù)程序?qū)嵤?;如網(wǎng)絡(luò)服務(wù),打印等。 下面這篇文章給大家分享了Python是如何實(shí)現(xiàn)守護(hù)進(jìn)程的方法示例,需要的朋友可以參考借鑒。2017-02-02
Python中的pyecharts庫(kù)使用總結(jié)
這篇文章主要介紹了Python中的pyecharts庫(kù)使用總結(jié),Pyecharts 提供了一個(gè)簡(jiǎn)單而直觀的 API 接口,使得使用者無需了解復(fù)雜的 JavaScript 語(yǔ)法,即可通過 Python 代碼實(shí)現(xiàn)高度定制化的圖表設(shè)計(jì),需要的朋友可以參考下2023-12-12
Python調(diào)用GPU算力的實(shí)現(xiàn)步驟
本文介紹了在Python中調(diào)用GPU算力的基本步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
Python利用glob庫(kù)實(shí)現(xiàn)輕松應(yīng)對(duì)文件和目錄管理
Python提供了glob庫(kù),它允許我們根據(jù)特定模式匹配文件和目錄,本文將詳細(xì)介紹glob庫(kù)的用法,并通過實(shí)例演示它的各種功能,需要的可以了解一下2023-07-07
Python使用Librosa進(jìn)行音頻處理操作詳解
Librosa是一個(gè)用于音頻和音樂分析的Python庫(kù),提供了豐富的功能來處理和分析音頻信號(hào),本文主要為大家介紹了如何使用Librosa進(jìn)行簡(jiǎn)單的音頻處理操作,需要的可以參考下2025-02-02
使用Python中wordcloud庫(kù)繪制詞云圖的詳細(xì)教程
這篇文章主要介紹了如何使用Python的wordcloud庫(kù)從Excel數(shù)據(jù)生成詞云圖,包括環(huán)境準(zhǔn)備、詞云圖的基本原理、生成詞云圖的步驟、保存詞云圖以及高級(jí)自定義(形狀與顏色),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12

