zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    003-notepad++插件
    002-notepad++语言,编码,字体,背景色
    001-notepad++下载安装、添加右键
    015-elasticsearch5.4.3【五】-搜索API【四】Joining 多文档查询、GEO查询、moreLikeThisQuery、script脚本查询、span跨度查询
    014-elasticsearch5.4.3【五】-搜索API【三】复合查询boolQuery、constantScoreQuery、disMaxQuery
    013-elasticsearch5.4.3【五】-搜索API【二】term术语查询-termQuery、rangeQuery、existsQuery、prefixQuery、wildcardQuery、regexpQuery、fuzzyQuery
    012-elasticsearch5.4.3【五】-搜索API【一】搜索匹配所有matchAllQuery、全文查询[matchQuery、multiMatchQuery、commonTermsQuery、queryStringQuery、simpleQueryStringQuery]
    011-elasticsearch5.4.3【四】-聚合操作【二】-桶聚合【bucket】过滤、嵌套、反转、分组、排序、范围
    010-elasticsearch5.4.3【四】-聚合操作【一】-度量聚合【metrics】-min、max、sum、avg、count
    009-elasticsearch5.4.3【三】搜索概述-查询模型、分页、ES数据类型
  • 原文地址:https://www.cnblogs.com/yibeimingyue/p/9381622.html
Copyright © 2011-2022 走看看