新聞中心
分享調(diào)用函數(shù)的9種方法:getattr, partial, eval, __dict__, globals, exec, attrgetter, methodcaller, 和 __call__。其中一些函數(shù)調(diào)用方法,在函數(shù)式編程或元編程場(chǎng)景中大量使用。相信你在今后的學(xué)習(xí)或工作中會(huì)遇到!

10年積累的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有鐵東免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
1. 直接調(diào)用函數(shù)(__call__)
最簡(jiǎn)單最直接的使用方法:
def func():
print('Hello, world!')
func() # Hello, world!
func.__call__() # 一樣的2. partial 函數(shù)
在python的內(nèi)置庫(kù)functools中有一個(gè)partial函數(shù),可以讓我們可以把一個(gè)函數(shù)的一部分參數(shù)填入,然后調(diào)用??雌饋?lái)沒(méi)什么用,遇到的時(shí)候有大用。
from functools import partial
# 請(qǐng)仔細(xì)品!
def func(domain, user):
echo = f"Hello, {user}@{domain}!"
print(echo)
func_userA = partial(func, user="userA")
func_userB = partial(func, user="userB")
func_userA("cdxwcx.com") # Hello, [email protected]!
func_userB("cdxwcx.com") # Hello, [email protected]!3. eval 函數(shù)
如果需要?jiǎng)討B(tài)執(zhí)行函數(shù),可以使用 eval 來(lái)執(zhí)行動(dòng)態(tài)代碼。
import sys
def pre_task():
print("running pre_task")
def task():
print("running task")
def post_task():
print("running post_task")
cmdList = ["pre_task()","task()","post_task()"]
for cmd in cmdList:
eval(cmd) # 執(zhí)行函數(shù)
# running pre_task
# running task
# running post_task4. getattr 函數(shù)
運(yùn)行類(lèi)中的靜態(tài)方法
import sys
class Task:
@staticmethod
def pre_task():
print("running pre_task")
@staticmethod
def task():
print("running task")
@staticmethod
def post_task():
print("running post_task")
#? 沒(méi)有括號(hào)的字符串。
cmdList = ["pre_task", "task", "post_task"]
task = Task()
for cmd in cmdList:
func = getattr(task, cmd)
func()5. dict 方法
首先我們需要知道,每個(gè)python對(duì)象都有一個(gè)內(nèi)置的__dict__()方法,這個(gè)方法返回一個(gè)字典,包含了對(duì)象的所有屬性。如下圖,我們可以看到list的__dict__()方法返回的所有屬性,其中紅框內(nèi)的,你是否有些熟悉?
class Task:
@staticmethod
def pre_task():
print("running pre_task")
@staticmethod
def task():
print("running task")
@staticmethod
def post_task():
print("running post_task")
func = Task.__dict__.get("pre_task")
func.__func__() # running pre_task
func() # 為什么不這樣用?6. globals 函數(shù)
import sys
def pre_task():
print("running pre_task")
def task():
print("running task")
def post_task():
print("running post_task")
cmdList = ["pre_task", "task", "post_task"]
for cmd in cmdList:
func = globals().get(cmd)
func()
# running pre_task
# running task
# running post_task7. exec 函數(shù)
你可以在一個(gè)字符串中定義你的函數(shù),并使用compile函數(shù)將它編譯成字節(jié)碼,然后使用exec來(lái)執(zhí)行它。
# 方式1
pre_task = """
print("running pre_task")
"""
exec(compile(pre_task, '', 'exec'))
# running pre_task
# 方式2
with open('./source.txt') as f:
source = f.read()
exec(compile(source, 'source.txt', 'exec'))8. attrgetter 函數(shù)
在內(nèi)置庫(kù)operator中,有一個(gè)獲取屬性的方法,叫做attrgetter,我們可以通過(guò)它獲取函數(shù)后執(zhí)行。
from operator import attrgetter
class People:
def speak(self, dest):
print("Hello, %s" %dest)
p = People()
caller = attrgetter("speak")
caller(p)("Tony") # Hello, Tony
# 本文第四條
caller2 = getattr(p, "speak")
caller2("Tony") # Hello, Tony9. methodcaller 函數(shù)
from operator import methodcaller
class People:
def speak(self, dest):
print(f"Hello, {dest}")
caller = methodcaller("speak", "Tony")
p = People()
caller(p)小節(jié)
總結(jié)下,本文分享了使用函數(shù)的9種方法:getattr, partial, eval, __dict__, globals, exec, attrgetter, methodcaller, 和 __call__。
請(qǐng)仔細(xì)品味,思考下他們的使用場(chǎng)景。其中一些函數(shù)調(diào)用方法,在函數(shù)式編程或元編程場(chǎng)景中大量使用。相信你在今后的學(xué)習(xí)或工作中會(huì)遇到!
新聞名稱(chēng):Python函數(shù)調(diào)用的九大方法,鮮為人知
網(wǎng)頁(yè)路徑:http://m.fisionsoft.com.cn/article/dpihgec.html


咨詢(xún)
建站咨詢(xún)
