python裝飾器 理解Python裝飾器

2021-10-11 19:51:11 字數 4367 閱讀 6144

在python中,對於乙個函式,若想在其執行前後做點什麼,那麼裝飾器是再好不過的選擇,話不多說,上**。

#!/usr/bin/env

# -*-coding:utf-8-*-

# script: 01.py

__author__ = 'howie'

from functools import wraps

def decorator(func):

@wraps(func)

print("%s was called" % func.__name__)

func(*args, **kwargs)

@decorator

def hello(name="howie"):

print("hello %s!" % name)

hello()

outputs:

hello was called

hello howie!

這段**,初看之下,確實不是很理解,接下來一步一步分析,看看裝飾器到底是怎麼工作的。

在python中,方法允許作為引數傳遞,想在某個函式執行前後加點料,也可以這樣簡單實現。

#!/usr/bin/env

# -*-coding:utf-8-*-

# script: 02-1.py

__author__ = 'howie'

def decorator(func):

print("%s was called" % func.__name__)

func()

def hello(name="howie"):

print("hello %s!" % name)

decorator(hello)

由此,上面**也可以這樣寫:

#!/usr/bin/env

# -*-coding:utf-8-*-

# script: 02-2.py

__author__ = 'howie'

def decorator(func):

print("%s was called" % func.__name__)

func()

@decorator

def hello(name="howie"):

print("hello %s!" % name)

hello

兩段**執行後:

outputs:

hello was called

hello howie!

表面上看來,02-2.py**看起來也可以很好地執行啊,可請注意,在末尾處,hello只是函式名稱,它並不能被呼叫,若執行hello(),就會報typeerror: 'nonetype' object is not callable物件不能呼叫錯誤,這是自然,在decoratorfunc()直接將傳入的函式例項化了,有人會想,那如果這樣改呢?

#!/usr/bin/env

# -*-coding:utf-8-*-

# script: 02-3.py

__author__ = 'howie'

def decorator(func):

print("%s was called" % func.__name__)

return func

@decorator

def hello(name="howie"):

print("hello %s!" % name)

hello()

確實,這樣改是可以,可有沒有想過,若想在函式執行結束後加點裝飾呢?這樣便行不通了,可能又有人會想,若這樣改呢?

#!/usr/bin/env

# -*-coding:utf-8-*-

# script: 02-4.py

__author__ = 'howie'

def decorator(func):

print("%s was called" % func.__name__)

func()

return bye

def bye():

print("bye~")

@decorator

def hello(name="howie"):

print("hello %s!" % name)

hello()

這樣寫看起來,恩,怎麼說呢,總有種沒有意義的感覺,不如直接將在外部的函式放進decorator中,如下:

#!/usr/bin/env

# -*-coding:utf-8-*-

# script: 02-5.py

__author__ = 'howie'

def decorator(func):

print("%s was called" % func.__name__)

func()

print("bye~")

@decorator

def hello(name="howie"):

print("hello %s!" % name)

hello()

執行:

outputs:

hello was called

hello howie!

bye~

#!/usr/bin/env

# -*-coding:utf-8-*-

# script: 02-6.py

__author__ = 'howie'

def decorator(func):

print("%s was called" % func.__name__)

func()

print("bye~")

@decorator

def hello(name="howie"):

print("hello %s!" % name)

hello()

print(hello.__name__)

outputs:

hello was called

hello howie!

bye~

#!/usr/bin/env

# -*-coding:utf-8-*-

# script: 02-7.py

__author__ = 'howie'

from functools import wraps

def decorator(func):

@wraps(func)

print("%s was called" % func.__name__)

func()

print("bye~")

@decorator

def hello(name="howie"):

print("hello %s!" % name)

hello()

print(hello.__name__)

執行**:

outputs:

hello was called

hello howie!

bye~

hello

functions.wraps作用是不是一目了然哈~到了這一步,再看01.py的**,是不是**結構清晰明了,只不過多了個引數~

#!/usr/bin/env

# -*-coding:utf-8-*-

# script: 01.py

__author__ = 'howie'

from functools import wraps

def decorator(func):

@wraps(func)

print("%s was called" % func.__name__)

func(*args, **kwargs)

@decorator

def hello(name="howie"):

print("hello %s!" % name)

hello('world')

猜都猜得到執行後輸出什麼了。

只要了解裝飾器原理,不管是帶引數的裝飾器,還是裝飾器類,都是小菜一碟。 若有錯誤,盡請指出。

python裝飾器理解 python裝飾器理解

裝飾器 在不改變原函式的 和呼叫方法的基礎上,給原函式增加額外的功能 理解宣告 為了方便理解,以下例子採用最簡潔的函式和新增的功能 給原函式新增乙個執行時間 import time def timer func def inner func return inner timer func timer...

python裝飾器理解 python裝飾器的理解

python裝飾器應該算是面試常考到的用點,之前在flask的應用中也是會常常用到,抽空仔細看書查資料理解了下裝飾器的概念,通過自己的理解記憶,應該對這個概念會有乙個大致上具體的了解。閉包說起python裝飾器,我們應該不得不談談閉包的概念。我對閉包的理解是,當函式存在巢狀,子函式呼叫了父函式的變數...

python裝飾器理解

裝飾器,網上有很多文章,描述裝飾器,看了總是半知半解,現在是終於明白裝飾器的用法了 deco arg def func argc,argv pass 等同於 equal 說明 後面的func是指原定義的func,前面的func是指使用裝飾器 裝飾 後,我們再去使用的func定義,有點拗口 func ...