python再议装饰器

装饰器实质还是一个函数,是对其他函数进行装饰的函数。装饰器函数接受被装饰函数的函数名,返回被装饰函数的函数名。对一个函数进行装饰有两个原则:一是不能修改被装饰函数的源代码;二是被装饰函数的调用方式不可以发生改变。

#用dec()函数对poem()函数进行装饰,但是调用方式由poem()变成了name(),
#调用方式发生了改变...这违反写装饰器的第二个原则。解决办法:把name=
#deco(poem)这一句的name改成poem.即:name=deco(name),见下一段代码。
def deco(func): #接收一个函数名,返回相同的函数名
    print('古诗欣赏') #添加装饰语句
    return func
def poem():
    print('月落乌啼霜满天,江枫渔火对愁眠。')
name = deco(poem) #传进去函数名poem,再把返回的函数名poem赋给name
name() #装饰之后,再调用被装饰函数
'''
古诗欣赏
月落乌啼霜满天,江枫渔火对愁眠。
'''
View Code
#用dec()函数对poem()函数进行装饰,但是调用方式由poem()变成了name(),
#调用方式发生了改变...这违反写装饰器的第二个原则。解决办法:把name=
#deco(poem)这一句的name改成poem.即:name=deco(name),见下一段代码。
def deco(func): #接收一个函数名,返回相同的函数名
    print('古诗欣赏') #添加装饰语句
    return func
def poem():
    print('月落乌啼霜满天,江枫渔火对愁眠。')
poem = deco(poem) #传进去函数名poem,再把返回的函数名poem赋给name
poem() #装饰之后,再调用被装饰函数
'''
古诗欣赏
月落乌啼霜满天,江枫渔火对愁眠。
'''
View Code

写带有@的装饰语句 :

def deco(func): #接收一个函数名,返回相同的函数名
    print('古诗欣赏') #装饰语句
    return func
@deco #在被装饰的函数的头顶写装饰语句,相当于poem = deco(poem)
def poem():
    print('月落乌啼霜满天,江枫渔火对愁眠。')
#poem = deco(poem) #传进去函数名poem,再把返回的函数名poem赋给name
poem() #装饰之后,再调用被装饰函数
'''
古诗欣赏
月落乌啼霜满天,江枫渔火对愁眠。
'''
View Code

一般要把装饰器写成嵌套函数:

#对无参数的函数进行装饰
def guess_win(func): #传进去一个函数名german_team,返回一个函数名rooftop_status
    def rooftop_status():
        result = func() #相当于result=german_team()
        print('天台已满,请排队!')
        return result  #处理被装饰的函数german_team(),然后返回处理后的结果
    return rooftop_status #执行完这句后,把新定义的函数名rooftop_status返回给german_team:
                          #german_team=rooftop_status
@guess_win #这句话相当于german_team=guess_win(gernam_team)
def german_team():
    print('德国必胜!')
    return '赢了会所嫩模!输了下海干活!'
x = german_team() #由于return rooftop_status的作用是:german_team=rooftop_status
print(x)
'''
德国必胜!
天台已满,请排队!
赢了会所嫩模!输了下海干活!
'''
View Code
#对带参数的函数进行装饰
def guess_win(func): #传进去一个函数名german_team,返回一个函数名rooftop_status
    def rooftop_status(*args, **kwargs):
        result = func(*args, **kwargs)
        print('天台已满,请排队!')
        return result
    return rooftop_status #执行完这句后,把新定义的函数名rooftop_status返回给german_team:
                          #german_team=rooftop_status
@guess_win #这句话相当于german_team=guess_win(gernam_team),调用guess_win()函数
def german_team(arg):
    print('%s必胜!'% arg)
    return '德国赢了会所嫩模!输了下海干活!'
@guess_win #这句话相当于spain_team=guess_win(spain_team),调用guess_win()函数
def spain_team(arg):
    print('%s必胜!'% arg)
    return '西班牙赢了会所嫩模!输了下海干活!'
x = german_team('德国')
y = spain_team('西班牙')
print(x)
print(y)
'''
德国必胜!
天台已满,请排队!
西班牙必胜!
天台已满,请排队!
德国赢了会所嫩模!输了下海干活!
西班牙赢了会所嫩模!输了下海干活!
'''
View Code
def decorator(F):
    def new_F(a, b):
        print("input", a, b)
        return F(a, b)
    return new_F
@decorator #相当于:square_sum=decorator(square_sum)
def square_sum(a, b):
    return a**2 + b**2
@decorator
def square_diff(a, b):
    return a**2 - b**2

print(square_sum(4, 3))#相当于:
# square_sum = decorator(square_sum)
# square_sum(3, 4)
print(square_diff(4, 3))
'''
input 4 3
25
input 4 3
7
'''
View Code
原文地址:https://www.cnblogs.com/yibeimingyue/p/9381622.html