python裝飾器詳解

2021-08-21 03:00:53 字數 3454 閱讀 1466

import time

deffunc

(): print("hello")

time.sleep(1)

print("world")

#原始侵入,篡改原函式

import time

deffunc

(): starttime = time.time()

print("hello")

time.sleep(1)

print("world")

endtime = time.time()

msecs = (endtime - starttime)*1000

print("time is %d ms" %msecs)

#避免直接侵入原函式修改,但是生效需要再次執行函式

import time

defdeco

(func):

starttime = time.time()

func()

endtime = time.time()

msecs = (endtime - starttime)*1000

print("time is %d ms" %msecs)

deffunc

(): print("hello")

time.sleep(1)

print("world")

if __name__ == '__main__':

f = func

deco(f)#只有把func()或者f()作為引數執行,新加入功能才會生效

print("f.__name__ is",f.__name__)#f的name就是func()

print()

#func()

#既不需要侵入,也不需要函式重複執行

import time

defdeco

(func):

def():

starttime = time.time()

func()

endtime = time.time()

msecs = (endtime - starttime)*1000

print("time is %d ms" %msecs)

@deco

deffunc

(): print("hello")

time.sleep(1)

print("world")

if __name__ == '__main__':

f = func #這裡f被賦值為func,執行f()就是執行func()

f()

#帶有引數的裝飾器

import time

defdeco

(func):

def(a,b):

starttime = time.time()

func(a,b)

endtime = time.time()

msecs = (endtime - starttime)*1000

print("time is %d ms" %msecs)

@deco

deffunc

(a,b):

print("hello,here is a func for add :")

time.sleep(1)

print("result is %d" %(a+b))

if __name__ == '__main__':

f = func

f(3,4)

#func()

#帶有不定引數的裝飾器

import time

defdeco

(func):

def(*args, **kwargs):

starttime = time.time()

func(*args, **kwargs)

endtime = time.time()

msecs = (endtime - starttime)*1000

print("time is %d ms" %msecs)

@deco

deffunc

(a,b):

print("hello,here is a func for add :")

time.sleep(1)

print("result is %d" %(a+b))

@deco

deffunc2

(a,b,c):

print("hello,here is a func for add :")

time.sleep(1)

print("result is %d" %(a+b+c))

if __name__ == '__main__':

f = func

func2(3,4,5)

f(3,4)

#func()

#多個裝飾器

import time

defdeco01

(func):

def(*args, **kwargs):

print("this is deco01")

starttime = time.time()

func(*args, **kwargs)

endtime = time.time()

msecs = (endtime - starttime)*1000

print("time is %d ms" %msecs)

print("deco01 end here")

defdeco02

(func):

def(*args, **kwargs):

print("this is deco02")

func(*args, **kwargs)

print("deco02 end here")

@deco01

@deco02

deffunc

(a,b):

print("hello,here is a func for add :")

time.sleep(1)

print("result is %d" %(a+b))

if __name__ == '__main__':

f = func

f(3,4)

#func()

'''this is deco01

this is deco02

hello,here is a func for add :

result is 7

deco02 end here

time is 1003 ms

deco01 end here

'''

python裝飾器 python 裝飾器詳解

def outer x def inner y return x y return inner print outer 6 5 11 如 所示,在outer函式內,又定義了乙個inner函式,並且inner函式又引用了外部函式outer的變數x,這就是乙個閉包了。在輸出時,outer 6 5 第乙個...

python裝飾器詳解 python裝飾器詳解

按照 python 的程式設計原則,當乙個函式被定義後,如要修改或擴充套件其功能應盡量避免直接修改函式定義的 段,否則該函式在其他地方被呼叫時將無法正常執行。因此,當需要修改或擴充套件已被定義的函式的功能而不希望直接修改其 時,可以使用裝飾器。先來看乙個簡單的例子 def func1 functio...

詳解Python裝飾器

裝飾器的難點 在梳理了裝飾器的整個內容之後,我認為難點不是裝飾器本身,而是直接呼叫被裝飾的函式,讓人無法理解背後究竟發生了什麼。一 引出裝飾器概念 引入問題 定義了乙個函式,想在執行時動態的增加功能,又不想改動函式本身的 示例 希望對下列函式呼叫增加log功能,列印出函式呼叫 def f1 x re...