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)到此這篇關于python 裝飾器 帶參數(shù)和不帶參數(shù)的文章就介紹到這了,更多相關python 裝飾器 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python繪制指數(shù)分布的概率密度函數(shù)圖
在數(shù)據(jù)科學和統(tǒng)計學中,指數(shù)分布是一種應用廣泛的連續(xù)概率分布,通常用于建模獨立隨機事件發(fā)生的時間間隔,本文將展示如何在Python中繪制指數(shù)分布的概率密度函數(shù)圖,需要的可以了解下2024-12-12
Python collections中的雙向隊列deque簡單介紹詳解
這篇文章主要介紹了Python collections中的雙向隊列deque簡單介紹詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
Python實現(xiàn)GUI學生管理系統(tǒng)的示例代碼
這篇文章主要為大家介紹了如何留Python語言實現(xiàn)簡易的GUI學生管理系統(tǒng),文中的示例代碼講解詳細,對我們學習Python有一定幫助,需要的可以參考下2022-06-06
基于Tensorflow搭建一個神經(jīng)網(wǎng)絡的實現(xiàn)
神經(jīng)網(wǎng)絡可能會讓人感到恐懼,特別是對于新手機器學習的人來說。這篇文章主要介紹了基于Tensorflow搭建一個神經(jīng)網(wǎng)絡的實現(xiàn),從入門開始,感興趣的可以了解一下2021-05-05
使用Python創(chuàng)建簡單的HTTP服務器的方法步驟
這篇文章主要介紹了使用Python創(chuàng)建簡單的HTTP服務器的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04

