欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python通過函數(shù)名調(diào)用函數(shù)的幾種場(chǎng)景

 更新時(shí)間:2020年09月23日 10:58:54   作者:諸子流  
這篇文章主要介紹了python通過函數(shù)名調(diào)用函數(shù)的幾種場(chǎng)景,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

一、說明

之前寫了一篇“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)文章!

相關(guān)文章

  • Python Flask入門

    Python Flask入門

    今天小編就為大家分享一篇Python Flask的入門教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-11-11
  • Python學(xué)習(xí)小技巧之列表項(xiàng)的推導(dǎo)式與過濾操作

    Python學(xué)習(xí)小技巧之列表項(xiàng)的推導(dǎo)式與過濾操作

    這篇文章主要給大家介紹了Python學(xué)習(xí)小技巧之列表項(xiàng)的推導(dǎo)式與過濾操作的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看把。
    2017-05-05
  • windows下python之mysqldb模塊安裝方法

    windows下python之mysqldb模塊安裝方法

    這篇文章主要介紹了windows下python之mysqldb模塊安裝方法,需要的朋友可以參考下
    2017-09-09
  • 深入理解python多線程編程

    深入理解python多線程編程

    進(jìn)程是資源分配的最小單位,他是操作系統(tǒng)進(jìn)行資源分配和調(diào)度運(yùn)行的基本單位。通俗理解:一個(gè)正在運(yùn)行的一個(gè)程序就是一個(gè)進(jìn)程,本文重點(diǎn)給大家介紹python多線程編程的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-04-04
  • Python使用代理抓取網(wǎng)站圖片(多線程)

    Python使用代理抓取網(wǎng)站圖片(多線程)

    Python作為一門功能強(qiáng)大的腳本語言,經(jīng)常被用來寫爬蟲程序,下面是使用Python通過代理進(jìn)行多線程抓取圖片,算是一個(gè)簡(jiǎn)易的python多線程爬蟲
    2014-03-03
  • Pyecharts繪制可視化地球?qū)崿F(xiàn)示例

    Pyecharts繪制可視化地球?qū)崿F(xiàn)示例

    這篇文章主要為大家介紹了Pyecharts繪制可視化地球?qū)崿F(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 利用python獲取Ping結(jié)果示例代碼

    利用python獲取Ping結(jié)果示例代碼

    這篇文章主要給大家介紹了關(guān)于利用python獲取Ping結(jié)果的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • Python實(shí)現(xiàn)電腦壁紙的采集與輪換效果

    Python實(shí)現(xiàn)電腦壁紙的采集與輪換效果

    這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)電腦壁紙的采集以及輪換效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下
    2022-04-04
  • Python判斷素?cái)?shù)的3種方法及for-else語句的用法介紹

    Python判斷素?cái)?shù)的3種方法及for-else語句的用法介紹

    素?cái)?shù)又叫質(zhì)數(shù),指的是>1的整數(shù)中,只能被1和這個(gè)數(shù)本身整除的數(shù),這篇文章主要給大家介紹了關(guān)于Python判斷素?cái)?shù)的3種方法及for-else語句的用法介紹的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Python實(shí)用日期時(shí)間處理方法匯總

    Python實(shí)用日期時(shí)間處理方法匯總

    這篇文章主要介紹了Python實(shí)用日期時(shí)間處理方法匯總,本文講解了獲取當(dāng)前datetime、獲取當(dāng)天date、獲取明天/前N天、獲取當(dāng)天開始和結(jié)束時(shí)間(00:00:00 23:59:59)、獲取兩個(gè)datetime的時(shí)間差、獲取本周/本月/上月最后一天等實(shí)用方法 ,需要的朋友可以參考下
    2015-05-05

最新評(píng)論