自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在 Python 中調(diào)用函數(shù)?九種方法任你挑選

開(kāi)發(fā) 后端
如果把所有的函數(shù)都放在類中,并定義為靜態(tài)方法,就可以使用getattr()get和調(diào)用它們。

1. 直接函數(shù)調(diào)用

這是最簡(jiǎn)單、最直觀的方式:

  1. def test(): 
  2.     print("This is a test") 
  3. test() 

2. 使用partial()函數(shù)

在 的內(nèi)置庫(kù)中functools,有一個(gè)專用于生成偏函數(shù)的偏函數(shù)partial。

  1. def power(x, n): 
  2.     s = 1 
  3.     while n > 0: 
  4.         nn = n - 1 
  5.         ss = s * x 
  6.     return s 
  7.  
  8.  
  9. from functools import partial 
  10.  
  11. power_2 = partial(power, n=2
  12. power_2(3)  # output: 9 
  13. power_2(4)  # output: 16 

3. 使用 eval()

如果需要?jiǎng)討B(tài)執(zhí)行函數(shù),可以使用 eval + string 來(lái)執(zhí)行函數(shù)。

  1. # demo.py 
  2. import sys 
  3.  
  4.  
  5. def pre_task(): 
  6.     print("running pre_task") 
  7.  
  8.  
  9. def task(): 
  10.     print("running task") 
  11.  
  12.  
  13. def post_task(): 
  14.     print("running post_task") 
  15.  
  16.  
  17. argvs = sys.argv[1:] 
  18.  
  19.  
  20. for action in argvs: 
  21.     eval(action)() 

執(zhí)行:

  1. $ python demo.py pre_task task post_task 
  2. running pre_task 
  3. running task 
  4. running post_task 

4. 使用 getattr()

如果把所有的函數(shù)都放在類中,并定義為靜態(tài)方法,就可以使用getattr()get和調(diào)用它們。

  1. import sys 
  2.  
  3.  
  4. class Task: 
  5.     @staticmethod 
  6.     def pre_task(): 
  7.         print("running pre_task") 
  8.  
  9.  
  10.     @staticmethod 
  11.     def task(): 
  12.         print("running task") 
  13.  
  14.  
  15.     @staticmethod 
  16.     def post_task(): 
  17.         print("running post_task") 
  18.  
  19.  
  20. argvs = sys.argv[1:] 
  21.  
  22.  
  23. task = Task() 
  24.  
  25.  
  26. for action in argvs: 
  27.     func = getattr(task, action) 
  28.     func() 

5. 使用 __dict__()

我們都知道對(duì)象有一個(gè)__dict__()魔法方法,它存儲(chǔ)任何對(duì)象的屬性和方法。

您可以調(diào)用類方法使用__dict__.get

  1. import sys 
  2.  
  3.  
  4. class Task: 
  5.     @staticmethod 
  6.     def pre_task(): 
  7.         print("running pre_task") 
  8.  
  9.  
  10. func = Task.__dict__.get("pre_task") 
  11. func.__func__() 
  12. # Output 
  13. $ python /tmp/demo.py 
  14. running pre_task 

6. 使用 global()

在 的內(nèi)置庫(kù)中functools,有一個(gè)專用于生成偏函數(shù)的偏函數(shù)partial。

  1. import sys 
  2.  
  3.  
  4. def pre_task(): 
  5.     print("running pre_task") 
  6.  
  7.  
  8. def task(): 
  9.     print("running task") 
  10.  
  11.  
  12. def post_task(): 
  13.     print("running post_task") 
  14.  
  15.  
  16. argvs = sys.argv[1:] 
  17.  
  18.  
  19. for action in argvs: 
  20.     globals().get(action)() 
  21. # Output 
  22. $ python /tmp/demo.py pre_task task post_task 
  23. running pre_task 
  24. running task 
  25. running post_task 

7. 從文本編譯和運(yùn)行

您可以在字符串中定義您的函數(shù),并使用該compile函數(shù)將其編譯為字節(jié)碼,然后用于exec執(zhí)行它。

  1. pre_task = ""
  2. print("running pre_task") 
  3. """ 
  4. exec(compile(pre_task, '', 'exec')) 
  5. # Or from a text file 
  6. with open('source.txt') as f: 
  7.     source = f.read() 
  8.     exec(compile(source, 'source.txt', 'exec')) 

8. 使用attrgetter()

在 的內(nèi)置庫(kù)中operator,有一個(gè)獲取屬性的方法,稱為attrgetter,獲取函數(shù)后執(zhí)行。

  1. from operator import attrgetter 
  2.  
  3.  
  4. class People: 
  5.     def speak(self, dest): 
  6.         print("Hello, %s" %dest) 
  7.  
  8.  
  9. p = People() 
  10. caller = attrgetter("speak") 
  11. caller(p)("Tony") 
  12. # Output 
  13. $ python /tmp/demo.py 
  14. Hello, Tony 

9. 使用methodcaller()

還有一個(gè)methodcaller方法在operator

  1. from operator import methodcaller 
  2.  
  3.  
  4. class People: 
  5.     def speak(self, dest): 
  6.         print("Hello, %s" %dest) 
  7.  
  8.  
  9. caller = methodcaller("speak", "Tony") 
  10. p = People() 
  11. caller(p) 
  12. # Output 
  13. $ python /tmp/demo.py 
  14. Hello, Tony 

 

責(zé)任編輯:趙寧寧 來(lái)源: Python學(xué)會(huì)
相關(guān)推薦

2020-12-11 16:39:16

程序員編程網(wǎng)站

2021-08-18 11:55:25

Python函數(shù)代碼

2020-09-19 18:03:42

Docker

2017-11-03 10:40:25

Python復(fù)制文件方法

2009-06-17 13:19:50

Java調(diào)用DLL

2023-03-07 15:47:15

2022-01-16 09:30:34

Ansible自動(dòng)化工具開(kāi)源

2010-04-07 15:47:32

Oracle 11g

2010-04-27 15:39:54

Oracle 11g

2010-04-16 09:06:03

2024-04-24 10:47:20

物聯(lián)網(wǎng)智能建筑

2022-01-17 21:11:32

Windows 11Windows微軟

2020-02-03 09:36:08

物聯(lián)網(wǎng)智慧城市IOT

2023-12-21 14:32:51

Python函數(shù)

2023-05-17 10:53:43

AICIO

2025-01-14 07:00:00

線程池ExecutorsJava

2018-11-05 14:53:14

Go函數(shù)代碼

2011-07-22 12:58:16

服務(wù)器管理Android

2012-03-27 10:08:08

JavaScript

2023-06-02 10:02:17

AICIO供應(yīng)商
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)