目录
  • 一、什么是函数装饰器
  • 二、函数装饰器的执行时机
  • 三、变量作用域
  • 四、闭包
  • 五、保留函数的元数据
  • 七、使用lru_cache缓存函数执行结果
  • 八、使用singledispatch实现泛型函数
  • 九、通过参数控制函数装饰器的行为

一、什么是函数装饰器

1.函数装饰器是python提供的一种增强函数功能的标记函数;

2.装饰器是可调用的函数对象,其参数是另一个函数(被装饰的函数);

我们可以使用修饰器来封装某个函数,从而让程序在执行这个函数之前与执行完这个函数之后,分别运行某些代码。这意味着,调用者传给函数的参数值、函数返回给调用者的值,以及函数抛出的异常,都可以由修饰器访问并修改。这是个很有用的机制,能够确保用户以正确的方式使用函数,也能够用来调试程序或实现函数注册功能,此外还有许多用途。

二、函数装饰器的执行时机

函数装饰器在被装饰函数编译解析之后直接执行,装饰器是按照从上到下执行的;

函数装饰器内部定义的返回函数依附在装饰器的执行环境中;

函数装饰器每次执行都会生成新的返回函数;

import sys

def dec1(m):
    print(f'{sys._getframe().f_code.co_name} is execute, arg {m.__name__}')
    def newm1():
        print(f'{sys._getframe().f_code.co_name}')

    return newm1;

@dec1
def m1():
    print(f'{sys._getframe().f_code.co_name}')

@dec1
def m11():
    print(f'{sys._getframe().f_code.co_name}')

if __name__ == '__main__':
    print(m1)
    print(m11)
    print(f'm1==m11:{m1==m11}')
    
# dec1 is execute, arg m1
# dec1 is execute, arg m11
# <function dec1.<locals>.newm1 at 0x7fdfa97d9160>
# <function dec1.<locals>.newm1 at 0x7fdfa97d91f0>
# m1==m11:false

三、变量作用域

python中将变量声明和赋值操作合一,很容易导致函数局部变量覆盖函数外的变量

b=6
def f():
    print(b)

f()

# 6
b=6
def f():
    print(b)
    b = 9

f()

# unboundlocalerror: local variable 'b' referenced before assignment

通过生成的字节码可以看到两者对变量b的处理的差异,前者直接load_global,后者是load_fast,但是给b赋值却在print之后导致报错;

from dis import dis

b=6
def f():
    print(b)
    # b = 9

dis(f)

 # 5           0 load_global              0 (print)
 #              2 load_global              1 (b)
 #              4 call_function            1
 #              6 pop_top
 #              8 load_const               0 (none)
 #             10 return_value
from dis import dis

b=6
def f():
    print(b)
    b = 9
    
#  5          0 load_global              0 (print)
#             2 load_fast                0 (b)
#             4 call_function            1
#             6 pop_top

#  6          8 load_const               1 (9)
#             10 store_fast               0 (b)
#             12 load_const               0 (none)
#             14 return_value

可以使用global来强制声明b是全局变量,然后就可以重新赋值了;

b=6
def f():
    global b
    print(b)
    b = 9

f()

# 6

四、闭包

闭包是是指可以访问非在函数体内定义的非全局变量的函数;

通过函数的__code__及__closure__可以看到局部变量和自由变量及闭包的情况;

def makesum():
    sum = [0]

    def s(val):
        sum[0] += val
        return sum[0]

    return s



s = makesum()
print(s(1))
print(s(2))
print(s.__code__.co_varnames)
print(s.__code__.co_freevars)
print(s.__closure__)
print(s.__closure__[0].cell_contents)


# 1
# 3
# ('val',)
# ('sum',)
# (<cell at 0x7f63321f1b20: list object at 0x7f63321e8a00>,)
# [3]

基于三中python变量作用域的缘故,上边的sum只能使用列表对象,python提供的nonlocal关键字可以直接使用int类型的变量;

def makesum():
    sum = 0

    def s(val):
        nonlocal sum
        sum += val
        return sum

    return s

s = makesum()
print(s(1))
print(s(2))
print(s.__code__.co_varnames)
print(s.__code__.co_freevars)
print(s.__closure__)
print(s.__closure__[0].cell_contents)


# 1
# 3
# ('val',)
# ('sum',)
# (<cell at 0x7f73e6a4ab20: int object at 0x7f73e6b47970>,)
# 3

五、保留函数的元数据

函数装饰器默认会使用返回的函数完全取代被装饰的函数,这样可能会导致序列化或者开发工具智能提示的问题;可以使用functools.wraps来保留原始函数的标准属性(name、module、__annotations__等);

import functools

def dec(func):
    def real():
        '''this is real function'''
        pass
    return real

def wrapsdec(func):
    @functools.wraps(func)
    def real():
        '''this is real function'''
        pass
    return real

@dec
def max(nums):
    '''this is max function'''
    pass

@wrapsdec
def sum(nums):
    '''this is sum function'''

print(max)
print(max.__name__)
print(max.__doc__)
print(help(max))
print()
print(sum)
print(sum.__name__)
print(sum.__doc__)
print(help(sum))


# <function dec.<locals>.real at 0x7f1bfd4241f0>
# real
# this is real function
# help on function real in module __main__:
# 
# real()
#     this is real function
# 
# none
# 
# <function sum at 0x7f1bfd424280>
# sum
# this is sum function
# help on function sum in module __main__:
# 
# sum(nums)
#     this is sum function
# 
# none

六、支持关键字参数、位置参数

def dec(func):
    def real(*args, **kwargs):
        result = func(*args, **kwargs)
        return result

    return real

@dec
def sum(*nums, **kwargs):
    s = 0
    for n in nums:
        s = s + n

    for a in kwargs.values():
        s = s + a
    return s

print(sum(1,2,3,first=1))

七、使用lru_cache缓存函数执行结果

lru_cache内部使用函数的参数作为key,使用dict进行缓存执行结果,减少重复计算;

默认支持缓存128条记录,超过后采用lru方式丢弃多余记录;

需要注意执行中不要改变参数,否则会影响字典key的有效性;

from functools import lru_cache

@lru_cache()
def fibonacci(n):
    print(f'fibonacci({n})')
    if n<2:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(6))

# fibonacci(6)
# fibonacci(5)
# fibonacci(4)
# fibonacci(3)
# fibonacci(2)
# fibonacci(1)
# fibonacci(0)
# 8

八、使用singledispatch实现泛型函数

python不支持方法或者函数的重载,我们无法单独定义不同参数类型的同名函数;

singledispatch提供了这样一种能力,其通过注册具体的参数类型和关联的函数;

我们可以在自己的模块定义自己的类型,并实现自己的自定义函数;

import math
import numbers
from functools import singledispatch



@singledispatch
def absall(obj):
    return abs(obj)

@absall.register(numbers.number)
def numabs(num):
    return abs(num)

@absall.register(numbers.complex)
def cabs(c):
    return math.sqrt(c.real*c.real + c.imag* c.imag)

class line:

    def __init__(self, startx, starty, endx, endy):
        self.startx = startx
        self.starty = starty
        self.endx = endx
        self.endy = endy

@absall.register(line)
def lineabs(line):
    y =line.endy - line.starty
    x = line.endx - line.startx
    return math.sqrt(x*x + y*y)

print(absall(-1.1))
print(absall(3+4j))

l = line(1,2,4,6)
print(absall(l))

# 1.1
# 5.0
# 5.0

九、通过参数控制函数装饰器的行为

函数装饰器本身不支持传递参数,解析源代码的时候,python会将被装饰的函数对象作为第一个参数传递给装饰器;

我们可以通过在函数装饰器外再嵌套一个函数工厂来承载装饰特定函数的时候设置的参数; 

def accesscontrol(checkuser=true, updatesession=true):

    def dec(func):
        def checkuserf():
            print('check user')
            return true

        def updatesessionf():
            print('update session')
            return true

        def access():
            if checkuser:
                checkuserf()

            if updatesession:
               updatesessionf()

            func()

        return access

    return dec

@accesscontrol()
def pushitem():
    print('pushitem')
    return true

@accesscontrol(checkuser=false)
def getitem():
    print('getitem')
    return true

# pushitem()
# print()
# getitem()
# 
# check user
# update session
# pushitem
# 
# update session
# getitem

以上就是python 函数装饰器应用教程的详细内容,更多关于python 函数装饰器的资料请关注www.887551.com其它相关文章!