1. 程式人生 > 實用技巧 >python 高階知識-裝飾器

python 高階知識-裝飾器

1、裝飾器(類似於Java裡的註解)

import time
def decorator(func):
    def wapper():
        print(time.time())
        func()
    return wapper

@decorator
def f1():
    print("This is a function named")

f1()
# anson@ansonwandeMacBook-Pro python_ToolCodes % python3 test30.py
# 1597046862.721708
# This is a function named

2、如果func是有1個入參的

import time
def decorator(func):
    def wapper(funcname):
        print(time.time())
        func(funcname)
    return wapper

@decorator
def f1(funcname):
    print("This is a function named"+funcname)

f1("test")
# anson@ansonwandeMacBook-Pro python_ToolCodes % python3 test30.py
# 1597046983.011766 # This is a function namedtest

3、如果func是有2個入參的

import time
def decorator(func):
    def wapper(funcname1,funcname2):
        print(time.time())
        func(funcname1,funcname2)
    return wapper

@decorator
def f1(funcname1,funcname2):
    print("This is a function named"+funcname1)
    
print("This is a function named"+funcname2) f1("test1","test2") # anson@ansonwandeMacBook-Pro python_ToolCodes % python3 test30.py # 1597047095.7641442 # This is a function namedtest1 # This is a function namedtest2

3、裝飾器優化1:延展一下,如果有多個入引數呢?而且,decorator也不止和f1()捆綁,還會和f2(),f3()捆綁之類的,如果這些有些無引數,有些有1個,有些有2個怎麼辦?*args 實現

import time
def decorator(func):
    def wapper(*args):
        print(time.time())
        func(*args)
    return wapper

@decorator
def f1(funcname1,funcname2):
    print("This is a function named"+funcname1)
    print("This is a function named"+funcname2)
f1("test1","test2")
# anson@ansonwandeMacBook-Pro python_ToolCodes % python3 test30.py
# 1597047287.727742
# This is a function namedtest1
# This is a function namedtest2

4、裝飾器優化2:以上裝飾器是不能相容關鍵字引數的,比如f3的關鍵字引數**kw

import time
def decorator(func):
    def wapper(*args):
        print(time.time())
        func(*args)
    return wapper

@decorator
def f1(funcname1,funcname2):
    print("This is a function named"+funcname1)
    print("This is a function named"+funcname2)
@decorator
def f2(funcname1,funcname2):
    print("This is a function named"+funcname1)
    print("This is a function named"+funcname2)
@decorator
def f3(funcname1,funcname2,**kw):
    print("This is a function named"+funcname1)
    print("This is a function named"+funcname2)
    print(kw)

f3("test1","test2",a=1,b=2,c='1,2,3')

#anson@ansonwandeMacBook-Pro python_ToolCodes % python3 test30.py
#Traceback (most recent call last):
#File "test30.py", line 22, in <module>
#f3("test1","test2",a=1,b=2,c='1,2,3')
#TypeError: wapper() got an unexpected keyword argument 'a'

怎麼解決這個問題呢? 新增一個形式引數,比如用**kw(注意,形式引數誰隨意命名的,這裡就用kw了)

import time
def decorator(func):
    def wapper(*args,**kw):
        print(time.time())
        func(*args,**kw)
    return wapper

@decorator
def f1(funcname1,funcname2):
    print("This is a function named"+funcname1)
    print("This is a function named"+funcname2)
@decorator
def f2():
    print("This is a function named")
    print("This is a function named")
@decorator
def f3(funcname1,funcname2,**kw):
    print("This is a function named"+funcname1)
    print("This is a function named"+funcname2)
    print(kw)

f3("test1","test2",a=1,b=2,c='1,2,3')
f1("test1","test2")
f2()
# anson@ansonwandeMacBook-Pro python_ToolCodes % python3 test30.py
# 1597047607.285459
# This is a function namedtest1
# This is a function namedtest2
# {'a': 1, 'b': 2, 'c': '1,2,3'}
# 1597047607.285517
# This is a function namedtest1
# This is a function namedtest2
# 1597047607.285526
# This is a function named