python装饰器之函数作用域

Posted zaorunzi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python装饰器之函数作用域相关的知识,希望对你有一定的参考价值。

1.函数作用域LEGB

L:local函数内部作用域

E:enclosing函数内部与内嵌函数之间

G:global全局作用域

B:build-in内置作用域

passline = 60
def func(val):
    passline = 90
    if val >= passline:
        print(pass)
    else:
        print(failed)
    def in_func()
        print(val)
    in_func()

def Max(val1,val2):
    return max(val1,val2)
func(89)
print(Max(90,100))

 

2.闭包理解与使用

闭包概念:Closure:内部函数中对enclosing作用域的变量进行引用

 

函数实质与属性

1:函数是一个对象

2:函数执行完成后内部变量回收

3:函数属性

4:函数返回值

闭包作用

1:封装

2:代码复用

passline = 60#100
def func(val):
    print(%x%id(val))
    passline = 90
    if val >= passline:
        print(pass)
    else:
        print(failed)
    def in_func():#(val,)
        print(val)
    in_func()
    return in_func


f = func(89)
f()
print(f.__closure__)


29198a8
failed
89
89
(<cell at 0x7fe62bbf42f0: int object at 0x29198a8>,)
[Finished in 0.0s]
def func_150(val):
    passline = 90
    if val >= passline:
        print(%d pass%val)
    else:
        print(failed)
    
def func_100(val):
    passline = 60
    if val >= passline:
        print(%d pass%val)
    else:
        print(failed)



def set_passline(passline):
    def cmp(val):
        if val >= passline:
            print(pass)
        else:
            print(failed)
    return cmp
f_100 = set_passline(60)
f_150 = set_passline(90)
print(type(f_100))
print(f_100.__closure__)

f_100(89)
f_100(59)
f_150(89)
f_150(59)
pass
failed
failed
failed
[Finished in 0.0s]
def my_averge(*arg):
    print(in my_averge)
    return sum(arg)/len(arg)

def my_sum(*arg):
    print(in my_sum)
    return sum(arg)

def dec(func):
    def in_dec(*arg):
        print(in dec arg = , arg)
        if len(arg) == 0:
            return 0
        for val in arg:
            if not isinstance(val, int):
                return 0
        return func(*arg)
    return in_dec

#dec return in_dec -> my_sum
#my_sum = in_dec(*arg)
my_sum = dec(my_sum)
print(my_sum(1, 2, 3, 4, 5))
print(my_sum(1, 2, 3, 4, 5, 6))
print(my_averge(1, 2, 3, 4, 5))
my_averge = dec(my_averge)
print(my_averge(1, 2, 3, 4, 5, 6))
(in dec arg = , (1, 2, 3, 4, 5))
in my_sum
15
(in dec arg = , (1, 2, 3, 4, 5, 6))
0
in my_averge
3
(in dec arg = , (1, 2, 3, 4, 5, 6))
0
[Finished in 0.0s]

3.装饰器

1:装饰器用来装饰函数

2:返回一个函数对象

3:被装饰函数标识符指向返回的函数对象

4:语法糖 @deco

def dec(func):
    print(call dec)
    def in_dec(*arg):#my_sum
        print(in dec arg = , arg)
        if len(arg) == 0:
            return 0
        for val in arg:
            if not isinstance(val, int):
                return 0
        return func(*arg)
    return in_dec

@dec
def my_sum(*arg):#my_sum = in_dec
    print(in my_sum)
    return sum(arg)

print(my_sum(1, 2, 3, 4, 5))

#my_sum = dec(my_sum)

 
#deco(bar) -> in_deco
#bar = in_deco
#bar() in_deco() bar()
def deco(func):
    def in_deco(x,y):
        print(in deco)
        func(x,y)
    print(call deco)
    return in_deco
@deco
def bar(x,y):
    print(in bar,x+y)
print(type(bar))
bar(1,2)
call dec
(in dec arg = , (1, 2, 3, 4, 5))
in my_sum
15
call deco
<type function>
in deco
(in bar, 3)
[Finished in 0.0s]

 




以上是关于python装饰器之函数作用域的主要内容,如果未能解决你的问题,请参考以下文章

Python装饰器之 property()

Python装饰器之 property()

学Python 函数从青铜到王者

学Python 函数从青铜到王者

python函数下篇装饰器和闭包,外加作用域

python函数作用域+装饰器