AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁 > 科技  > 軟件

Python 中的 @wraps 到底是個(gè)啥東西?

來源: 責(zé)編: 時(shí)間:2024-07-12 17:21:21 96觀看
導(dǎo)讀只要你讀得很快你可能在隨意的 Python 代碼中見過這個(gè) @wraps 的東西,你可能想知道這到底是什么?函數(shù)有元數(shù)據(jù)元數(shù)據(jù)指的是函數(shù)本身的數(shù)據(jù)。def apple(): '''a function that prints apple''' print('apple')print(ap

只要你讀得很快

你可能在隨意的 Python 代碼中見過這個(gè) @wraps 的東西,你可能想知道這到底是什么?pq428資訊網(wǎng)——每日最新資訊28at.com

函數(shù)有元數(shù)據(jù)

元數(shù)據(jù)指的是函數(shù)本身的數(shù)據(jù)。pq428資訊網(wǎng)——每日最新資訊28at.com

def apple():  '''a function that prints apple'''  print('apple')print(apple.__name__)  # appleprint(apple.__doc__)   # 打印apple的函數(shù)

例子包括函數(shù)名 .__name__ 或函數(shù) docstring .__doc__pq428資訊網(wǎng)——每日最新資訊28at.com

裝飾器如何工作

裝飾器用于改變函數(shù)的行為方式,而無需修改任何源代碼。pq428資訊網(wǎng)——每日最新資訊28at.com

def greet(name):    return 'hello ' + nameprint(greet('tom'))  # hello tom

在這里,我們有一個(gè)普通的 greet 功能pq428資訊網(wǎng)——每日最新資訊28at.com

def add_exclamation(func):    def wrapper(name):        return func(name) + '!'    return wrapper@add_exclamationdef greet(name):    return 'hello ' + nameprint(greet('tom'))  # hello tom!

我們通過在 greet() 上添加 @add_exclamation 來用 add_exclamation() 來裝飾 greet()。這里,add_exclamation 是裝飾器,greet 是被裝飾的函數(shù)。pq428資訊網(wǎng)——每日最新資訊28at.com

請(qǐng)注意,greet() 的行為已經(jīng)改變,而我們根本沒有編輯 greet() 的源代碼。這是裝飾器的功勞。pq428資訊網(wǎng)——每日最新資訊28at.com

裝飾語法魔術(shù)

def add_exclamation(func):    def wrapper(name):        return func(name) + '!'    return wrapper@add_exclamationdef greet(name):    return 'hello ' + nameprint(greet('tom'))  # hello tom!

這是完全相同的:pq428資訊網(wǎng)——每日最新資訊28at.com

def add_exclamation(func):    def wrapper(name):        return func(name) + '!'    return wrapperdef greet(name):    return 'hello ' + namegreet = add_exclamation(greet)print(greet('tom'))  # hello tom!

注意 “greet = add_exclamation(greet) ”一行。pq428資訊網(wǎng)——每日最新資訊28at.com

裝飾會(huì)導(dǎo)致元數(shù)據(jù)丟失

# 沒有裝飾def greet(name):  '''says hello to someone'''  return 'hello ' + nameprint(greet.__name__)  # greetprint(greet.__doc__)   # says hello to someone

在這里,我們可以順利打印 greet() 的元數(shù)據(jù)。pq428資訊網(wǎng)——每日最新資訊28at.com

# 加了裝飾def add_exclamation(func):    def wrapper(name):        return func(name) + '!'    return wrapper@add_exclamationdef greet(name):  '''says hello to someone'''  return 'hello ' + nameprint(greet.__name__)  # wrapperprint(greet.__doc__)   # None

用 add_exclamation 裝飾 greet 后,請(qǐng)注意元數(shù)據(jù)發(fā)生了變化。__name__ 變成了 “wrapper”,而 __doc__ 變成了 wrapper 的 docstring。pq428資訊網(wǎng)——每日最新資訊28at.com

這是因?yàn)楫?dāng)我們裝飾 greet 時(shí),我們實(shí)際上是在做這件事:pq428資訊網(wǎng)——每日最新資訊28at.com

greet = add_exclamation(greet)

我們正在將 greet 重新分配給一個(gè)由 add_exclamation 返回的函數(shù)--wrapper。pq428資訊網(wǎng)——每日最新資訊28at.com

這就是為什么當(dāng)我們嘗試打印 greet.__name__ 和 greet.__doc__ 時(shí),會(huì)打印 wrapper 的元數(shù)據(jù)的原因。pq428資訊網(wǎng)——每日最新資訊28at.com

@wraps 防止元數(shù)據(jù)在裝飾過程中丟失

from functools import wrapsdef add_exclamation(func):    @wraps(func)    def wrapper(name):        return func(name) + '!'    return wrapper@add_exclamationdef greet(name):  '''says hello to someone'''  return 'hello ' + nameprint(greet.__name__)  # greetprint(greet.__doc__)   # says hello to someone

請(qǐng)注意,盡管使用了 ad_exclamation 裝飾,greet 的元數(shù)據(jù)還是回到了正常狀態(tài)。pq428資訊網(wǎng)——每日最新資訊28at.com

更具體地說,@wraps(something) 用 something 的元數(shù)據(jù)覆蓋了函數(shù)的元數(shù)據(jù)。這樣,我們?cè)瓉砗瘮?shù)的元數(shù)據(jù)就不會(huì)丟失了。pq428資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-100714-0.htmlPython 中的 @wraps 到底是個(gè)啥東西?

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 聊聊2024年Rust加密生態(tài)系統(tǒng)

下一篇: 使用 Docker 搭建 NodeJS 開發(fā)環(huán)境是一種什么體驗(yàn)?

標(biāo)簽:
  • 熱門焦點(diǎn)
Top