python装饰器学习笔记

LEGB:优先级依次递减

函数内部作用域,函数内部与内嵌函数之间,全局作用域,内置作用域

passline=60

def func(val):
    passline=90
    if val>=passline:
        print('pass')
    else:
        print('field')
    def in_func():
        print('输入了',val)
    in_func()

def Max(va1,va2):
    return max(va1,va2)

func(89)
print(Max(90,100))

闭包:内部函数中对enclosing(E)作用域的变量进行引用

def func_150(val):
    passline=90
    print('%x'%id(val))#会将变量添加到函数属性中
    """5c8e78e0"""
    if val>=passline:
        print('%dpass'%val)
    else:
        print('field')
    # def in_func():
    #     print('输入了',val)
    # in_func()
    # return in_func
'''(<cell at 0x00000285BAC1A648: int object at 0x000000005C8E78E0>,)'''
def func_100(val):
    passline=60
    print('%x'%id(val))
    if val>=passline:
        print('%dpass'%val)
    else:
        print('field')

def set_passline(passline):#passline=60
    def cmp(val):
        if val>=passline:
            print('Pass')
        else:
            print('fail')
    return cmp

# f=func(89)
# f()
# print(f.__closure__)
# func_100(89)
# func_150(89)

f_100=set_passline(60)
print(type(f_100))
print(f_100.__closure__)
'''<class 'function'>
(<cell at 0x000002C8B82AA648: int object at 0x000000005C8E7540>,)'''
f_100(89)
f_100(59)

#闭包可以实现封装和代码复用

def my_sum(*arg):
    print('in my sum')
    # if len(arg)==0:
    #     return 0
    # for i in arg:
    #     if not isinstance(i,int):
    #         return 0
    return sum(arg)
def my_avg(*arg):
    print('in my_avg')
    # if len(arg)==0:
    #     return 0
    # for i in arg:
    #     if not isinstance(i,int):
    #         return 0
    return sum(arg)/len(arg)

def dec(func):#缓存函数方法
    def in_dec(*arg):#My_sum会放到这个属性中
        print('in dec arg=',arg)
        if len(arg) == 0:
            return 0
        for i in arg:
            if not isinstance(i, int):
                return 0
        return func(*arg)#调用函数方法
    return in_dec
#my_sum = in_dec(*arg)
#先调用dec 返回in_dec --->my_sum
#第二步赋值 my_sum发生变化,本质上等于in_dec
my_sum=dec(my_sum)
my_avg=dec(my_avg)
'''调用dec,my_sum传入,再使用内嵌函数'''
'''实际上调用的函数等于内嵌函数'''
print(my_sum(1,2,3,4))
print(my_avg(1,2,3,4))

装饰器用来装饰对象,语法糖,返回一个函数对象

def dec(func):#缓存函数方法
    print('call dec')
    def in_dec(*arg):#My_sum会放到这个属性中
        print('in dec arg=',arg)
        if len(arg) == 0:
            return 0
        for i in arg:
            if not isinstance(i, int):
                return 0
        return func(*arg)#调用函数方法
    print('return in_dec')
    return in_dec#返回闭包
#其实就是条件过滤器
@dec#-->返回一个函数对象,就是闭包
def my_sum(*arg):
    print('in my sum')
    # if len(arg)==0:
    #     return 0
    # for i in arg:
    #     if not isinstance(i,int):
    #         return 0
    return sum(arg)

 

def deco(func):
    print('in deco')
    def bar(x,y):
        '''实际上是SSum'''
        print(bar.__closure__)
        print('in bar')
        func(x,y)
    return bar

#
@deco
def SSum(x,y):
    print(SSum.__closure__)
    print('加法:',x+y)

SSum(1,2)

in deco
(<cell at 0x0000015462C7A648: function object at 0x000001546318D510>, <cell at 0x0000015462C7A9A8: function object at 0x000001546318D488>)
in bar
(<cell at 0x0000015462C7A648: function object at 0x000001546318D510>, <cell at 0x0000015462C7A9A8: function object at 0x000001546318D488>)
加法: 3

阅读更多

没有更多推荐了,返回首页