日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python函數(shù)調(diào)用的九大方法,鮮為人知

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

10年積累的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(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__)

最簡單最直接的使用方法:

def func():
    print('Hello, world!')
func() # Hello, world!

func.__call__() # 一樣的

2. partial 函數(shù)

在python的內(nèi)置庫functools中有一個(gè)partial函數(shù),可以讓我們可以把一個(gè)函數(shù)的一部分參數(shù)填入,然后調(diào)用??雌饋頉]什么用,遇到的時(shí)候有大用。

from functools import partial
# 請仔細(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, userA@cdxwcx.com!
func_userB("cdxwcx.com") # Hello, userB@cdxwcx.com!

3. eval 函數(shù)

如果需要?jiǎng)討B(tài)執(zhí)行函數(shù),可以使用 eval 來執(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_task

4. getattr 函數(shù)

運(yùn)行類中的靜態(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")

#? 沒有括號(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_task

7. exec 函數(shù)

你可以在一個(gè)字符串中定義你的函數(shù),并使用compile函數(shù)將它編譯成字節(jié)碼,然后使用exec來執(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)置庫operator中,有一個(gè)獲取屬性的方法,叫做attrgetter,我們可以通過它獲取函數(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, Tony

9. 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__。

請仔細(xì)品味,思考下他們的使用場景。其中一些函數(shù)調(diào)用方法,在函數(shù)式編程或元編程場景中大量使用。相信你在今后的學(xué)習(xí)或工作中會(huì)遇到!


本文標(biāo)題:Python函數(shù)調(diào)用的九大方法,鮮為人知
路徑分享:http://www.5511xx.com/article/dpihgec.html