python 執(zhí)行函數(shù)的九種方法
這種是最簡單且直觀的方法
def task(): print('running task')task()
如果是在類中,也是如此
class Task: def task(self):print('running task')Task().task()方法二:使用偏函數(shù)來執(zhí)行
在 functools 這個內(nèi)置庫中,有一個 partial 方法專門用來生成偏函數(shù)。
def power(x, n): s = 1 while n > 0:n = n - 1s = s * x return sfrom functools import partialpower_2=partial(power, n=2)power_2(2) # output: 4power_2(3) # output: 9方法三:使用 eval 動態(tài)執(zhí)行
如果你有需要動態(tài)執(zhí)行函數(shù)的需要,可以使用 eval + 字符串 來執(zhí)行函數(shù)。
import sysdef pre_task(): print('running pre_task')def task(): print('running task')def post_task(): print('running post_task')argvs = sys.argv[1:]for action in argvs: eval(action)()
運行效果如下
$ python demo.py pre_task task post_taskrunning pre_taskrunning taskrunning post_task方法四:使用 getattr 動態(tài)獲取執(zhí)行
若把所有的函數(shù)是放在類中,并定義成靜態(tài)方法,那就不需要用 eval 了,接著使用 getattr 去獲取并調(diào)用。
import sysclass Task: @staticmethod def pre_task():print('running pre_task') @staticmethod def task():print('running task') @staticmethod def post_task():print('running post_task')argvs = sys.argv[1:]task = Task()for action in argvs: func = getattr(task, action) func()方法五:使用類本身的字典
我們都知道對象都有一個 __dict__() 的魔法方法,存放所有對象的屬性及方法。
到這里,大家可以思考一下, 如果還是上面的代碼,我直接取實例的 __dict__() 能不能取到函數(shù)呢?
我相信很多人都會答錯。
上面我們定義的是靜態(tài)方法,靜態(tài)方法并沒有與實例進行綁定,因此靜態(tài)方法是屬于類的,但是不是屬于實例的,實例雖然有使用權(quán)(可以調(diào)用),但是并沒有擁有權(quán)。
因此要想通過 __dict__ 獲取函數(shù),得通過類本身 Task,取出來的函數(shù),調(diào)用方法和平時的也不一樣,必須先用 __func__ 獲取才能調(diào)用。
import sysclass Task: @staticmethod def pre_task():print('running pre_task')func = Task.__dict__.get('pre_task')func.__func__()方法六:使用 global() 獲取執(zhí)行
上面放入類中,只是為了方便使用 getattr 的方法,其實不放入類中,也是可以的。此時你需要借助 globals() 或者 locals() ,它們本質(zhì)上就是一個字典,你可以直接 get 來獲得函數(shù)。
import sysdef pre_task(): print('running pre_task')def task(): print('running task')def post_task(): print('running post_task')argvs = sys.argv[1:]for action in argvs: globals().get(action)()方法七:從文本中編譯運行
先定義一個字符串,內(nèi)容是你函數(shù)的內(nèi)容,比如上面的 pre_task ,再通過 compile 函數(shù)編進 編譯,轉(zhuǎn)化為字節(jié)代碼,最后再使用 exec 去執(zhí)行它。
pre_task = '''print('running pre_task')'''exec(compile(pre_task, ’<string>’, ’exec’))
若你的代碼是放在一個 txt 文本中,雖然無法直接導入運行,但仍然可以通過 open 來讀取,最后使用 compile 函數(shù)編譯運行。
with open(’source.txt’) as f: source = f.read() exec(compile(source, ’source.txt’, ’exec’))方法八:使用 attrgetter 獲取執(zhí)行
在 operator 這個內(nèi)置庫中,有一個獲取屬性的方法,叫 attrgetter ,獲取到函數(shù)后再執(zhí)行。
from operator import attrgetterclass People: def speak(self, dest):print('Hello, %s' %dest)p = People()caller = attrgetter('speak')caller(p)('明哥')方法九:使用 methodcaller 執(zhí)行
同樣還是 operator 這個內(nèi)置庫,有一個 methodcaller 方法,使用它,也可以做到動態(tài)調(diào)用實例方法的效果。
from operator import methodcallerclass People: def speak(self, dest):print('Hello, %s' %dest)caller = methodcaller('speak', '明哥')p = People()caller(p)
以上就是我總結(jié)的函數(shù)執(zhí)行的十種方法,很多方法,大家也都知道,但是也有幾個方法,幾乎是見不到的,尤其是后面使用 operator 庫的那兩種方法。
以上就是python 執(zhí)行函數(shù)的九種方法的詳細內(nèi)容,更多關(guān)于python 執(zhí)行函數(shù)的方法的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. PHP laravel實現(xiàn)導出PDF功能2. JavaScript實現(xiàn)留言板實戰(zhàn)案例3. 使用Blazor框架實現(xiàn)在前端瀏覽器中導入和導出Excel4. ASP基礎(chǔ)知識Command對象講解5. python中文本字符處理的簡單方法記錄6. 資深程序員:給Python軟件開發(fā)測試的25個忠告!7. Python-openpyxl表格讀取寫入的案例詳解8. 如何從Python的cmd中獲得.py文件參數(shù)9. Python基于requests庫爬取網(wǎng)站信息10. vscode運行php報錯php?not?found解決辦法

網(wǎng)公網(wǎng)安備