python 裝飾器帶參數(shù)和不帶參數(shù)步驟詳解
裝飾器是Python語言中一種特殊的語法,用于在不修改原函數(shù)代碼的情況下,為函數(shù)添加額外的功能或修改函數(shù)的行為。通過裝飾器,我們可以在函數(shù)執(zhí)行前后執(zhí)行一些額外的代碼,或者修改函數(shù)的參數(shù)。
要使用裝飾器引入函數(shù)和參數(shù),可以按照以下步驟進行:
- 定義裝飾器函數(shù):裝飾器函數(shù)是一個普通的Python函數(shù),它接受一個函數(shù)作為參數(shù),并返回一個新的函數(shù)。裝飾器函數(shù)通常使用@符號放在被裝飾函數(shù)的定義之前,表示該函數(shù)將被裝飾。
- 在裝飾器函數(shù)內(nèi)部定義新的函數(shù):在裝飾器函數(shù)內(nèi)部,可以定義一個新的函數(shù),用于包裹原函數(shù),并在包裹函數(shù)中添加額外的功能。
- 在包裹函數(shù)中調(diào)用原函數(shù):在包裹函數(shù)中,可以調(diào)用原函數(shù),并傳遞原函數(shù)的參數(shù)。
- 返回包裹函數(shù):在包裹函數(shù)的最后,需要返回包裹函數(shù)本身。
全局定義參數(shù)傳參
def decorator_function(names):
def inner_wrapper(func):
def wrapper():
result = func()
return result + f"""my class student's name is {"、".join(names)};"""
return wrapper
return inner_wrapper
names = ["Mike", "David", "Jhon"]
@decorator_function(names)
def generate_code():
return f" I'm a teacher! "
result_str = generate_code()
print(result_str)不帶參數(shù),這里的names為全局參數(shù)
def subnormal_saturation_decorator(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result + f"""my class student's name is {"、".join(names)};"""
return wrapper
names = ["Mike", "David", "Jhon"]
@subnormal_saturation_decorator
def generate_code():
return f" I'm a teacher!"
result_str = generate_code()
print(result_str)通過方法傳參
def subnormal_saturation_decorator(func):
def wrapper(names,*args, **kwargs):
result = func(names,*args, **kwargs)
return result + f"""my class student's name is {"、".join(names)};"""
return wrapper
@subnormal_saturation_decorator
def generate_code(names):
print(names)
return f" I'm a teacher!"
result_str = generate_code(["Mike", "David", "Jhon"])
print(result_str)調(diào)用使用裝飾器的方法,帶傳參
# 裝飾器
def subnormal_saturation_decorator(func):
def wrapper(names,*args, **kwargs):
result = func(names,*args, **kwargs)
return result + f"""my class student's name is {"、".join(names)};"""
return wrapper
# 使用裝飾器的方法
@subnormal_saturation_decorator
def generate_code(names):
print(names)
return f" I'm a teacher!"
# 調(diào)用使用裝飾器方法的方法
def supper_func():
names = ["Mike", "David", "Jhon"]
return generate_code(names)
result_str = supper_func()
print(result_str)到此這篇關(guān)于python 裝飾器 帶參數(shù)和不帶參數(shù)的文章就介紹到這了,更多相關(guān)python 裝飾器 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python3中bytes和string之間的互相轉(zhuǎn)換
這篇文章主要介紹了python3中bytes和string之間的互相轉(zhuǎn)換,文中給出了詳細的介紹和示例代碼,相信對大家具有一定的參考價值,有需要的朋友們下面來一起學習學習吧。2017-02-02
使用python實現(xiàn)一個簡單ping?pong服務(wù)器
這篇文章主要為大家介紹了使用python實現(xiàn)一個簡單ping?pong服務(wù)器,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
pandas factorize實現(xiàn)將字符串特征轉(zhuǎn)化為數(shù)字特征
今天小編就為大家分享一篇pandas factorize實現(xiàn)將字符串特征轉(zhuǎn)化為數(shù)字特征,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
使用 NumPy 和 Matplotlib 繪制函數(shù)圖
Matplotlib 是 Python 的繪圖庫。 它可與 NumPy 一起使用,提供了一種有效的 MatLab 開源替代方案。 它也可以和圖形工具包一起使用,如 PyQt 和 wxPython2021-09-09
Python模擬登錄requests.Session應(yīng)用詳解
這篇文章主要介紹了Python模擬登錄requests.Session應(yīng)用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11

