python中的装饰器原理和作用
Posted python学习者0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中的装饰器原理和作用相关的知识,希望对你有一定的参考价值。
装饰器的作用就是用一个新函数封装旧函数(是旧函数代码不变的情况下增加功能)然后会返回一个新函数,新函数就叫做装饰器,一般为了简化装饰器会用语法糖@新函数来简化
例子:
这是一段代码,但功能太少,要对这个进行增强,但又不能改变代码。
def hello():
return "hello world!"
现在我们的需求是要增强hello()函数的功能,希望给返回加上HTML标签,比如hello world,但要求我们不得改变hello()函数原来的定义。
def makeitalic(fun):#makitalic传了一个新函数
def wrapped():#内部函数
return "<i>"+fun()+"</i>"#要加的新功能
return wrapped#返回的是wrapped函数功能
def hello():#对这个功能进行增强
return "hello world!"
#makeitalic里面传入了hello函数,然后内部函数fun()函数也就相当于hello函数了
hello_2=makeitalic(hello)
#打印新函数,返回的就是<i>hello world!</i>
print(hello_2())
为了增强原函数hello的功能,定义了一个函数,它接收原函数作为参数,并返回一个新的函数,在这个返回的函数中,执行了原函数,并对原函数的功能进行了增强。
事实上,makeitalic就是一个装饰器(decorator),它封装了原函数hello,并返回了一个新函数,用于增强原函数的功能,并将其赋值给hello。
一般情况下,我们使用装饰器提供的@语法糖(Syntactic Sugar),来简化上面的操作。
####使用@语法糖
def makeitalic(fun):
def wrapped():
return "<i>" + fun() + "</i>"
return wrapped
@makeitalic#使用了装饰器可以直接调用,不需要赋值了
def hello():
return "hello world"
print(hello())#使用了装饰器可以直接调用,不需要赋值了
像上面的情况,可以动态的修改函数(或类的)功能的函数就是装饰器。本质上,它是一个高阶函数,以被装饰的函数(比如上面的hello)为参数,并返回一个包装后的函数(比如上面的wrapped)给被修饰函数(hello)。
当调用hello()函数时,hello函数的执行流程如下分析:
1.把hello函数作为参数传给@符号后面的装饰器函数。
2.然后开始执行装饰器函数,并返回一个包装了的函数,同时,改变原函数的指向,现在原函数指向了这个包装函数。
3.执行原函数,其实此时执行的是包装了的函数,所以说,装饰器增强了一个现有函数的功能,但不会改变现有函数的定义。
普通装饰器的使用形式:
@decorator
def fun():
pass
#格式就如同下面的:
#Python小白交流学习群:711312441
def fun():
pass
fun = decorator(fun)#不使用语法糖要进行赋值
装饰器可以定义多个,离函数定义最近的装饰器最先被调用,比如:
@decotator_one
@decorator_two
def fun():
pass
#格式如同下面的:
def fun():
pass
fun = decorator_one(decorator_two(fun))
装饰器还可以带参数,比如:
@decorator(arg1, arg2)
def fun():
pass
#格式如同下面的:
def fun():
pass
fun = decorator(arg1, arg2)(fun)
装饰器详解
装饰器的原理
装饰器原理就是利用闭包函数来实现,而闭包函数就是内层函数的return和外层函数的作用域名字的引用。
装饰器之所以能这么方便的应用,就是因为python的“一切皆对象”。
为什么用装饰器
能让我们在开发过程中的这些事变的容易:
- 分析、日志和手段
- 验证及运行检查
- 创建框架
- 复用不能复用的代码
装饰器的基本用法
@符号是装饰器的语法糖,在定义函数的时候使用,可以避免再次的赋值操作。
@outter等同于foo=outter(foo)
带参装饰器
def use_logging(level):
def decorator(func):
def wrapper(*args, **kwargs):
if level == "warn":
logging.warn("%s is running" % func.__name__)
return func(*args)
return wrapper
return decorator
@use_logging(level="warn")
def foo(name=‘foo‘):
print("i am %s" % name)
foo()
多个装饰器的执行顺序
def decorator_a(func):
print ‘Get in decorator_a‘
def inner_a(*args, **kwargs):
print ‘Get in inner_a‘
return func(*args, **kwargs)
return inner_a
def decorator_b(func):
print ‘Get in decorator_b‘
def inner_b(*args, **kwargs):
print ‘Get in inner_b‘
return func(*args, **kwargs)
return inner_b
@decorator_b
@decorator_a
def f(x):
print ‘Get in f‘
return x * 2
f(1)
两个函数decotator_a,decotator_b的功能是接收函数作为参数f,然后返回给另一个函数他们各自的inner,在创建的函数inner里调用接收的函数f。
执行结果为:
Get in decorator_a
Get in decorator_b
Get in inner_b
Get in inner_a
Get in f
函数和函数调用:函数在python中也是一个对象,因此函数f是一个对象,其值是函数本身。函数调用f(1)是对函数f传入的参数进行求值得结果。因此,decorator_a函数的返回值是inner_a,inner_a函数在decorator_a里定义。在inner_a里调用f,将f的调用结果作为返回值返回。
用加上一些测试,再debug执行一下,看执行顺序。
def decorator_a(func):
print(‘Get in decorator_a‘)
print(func)#拿到的是<function f at 0x02AB45D0>
def inner_a(*args, **kwargs):
print(‘Get in inner_a‘)
return func(*args, **kwargs)
return inner_a
def decorator_b(func):
print(‘Get in decorator_b‘)
print(func)#拿到的是<function decorator_a.<locals>.inner_a at 0x02AB44B0>
def inner_b(*args, **kwargs):
print(‘Get in inner_b‘)
return func(*args, **kwargs)
return inner_b
@decorator_b#等于f=decorator_b(f)
@decorator_a
def f(x):
print(‘Get in f‘)
return x * 2
f(1)
结果:
Get in decorator_a
<function f at 0x02AB45D0>
Get in decorator_b
<function decorator_a.<locals>.inner_a at 0x02AB44B0>
Get in inner_b
Get in inner_a
Get in f
装饰类的装饰器
class Foo(object):
def __init__(self, func):
self._func = func
def __call__(self):
print (‘class decorator runing‘)
self._func()
print (‘class decorator ending‘)
@Foo
def bar():
print (‘bar‘)
bar()
以上是关于python中的装饰器原理和作用的主要内容,如果未能解决你的问题,请参考以下文章