“`” 含义:装饰器本质就是函数,为其他函数添加附加功能

原则:

不修改被修饰函数的代码

不修改被修饰函数的调用方式

应用场景:

无参装饰器在用户登录 认证中常见

有参装饰器在flask的路由系统中见到过

<pre><code>import functools
def wrapper(func):
@functools.wraps(func)
def inner(*args, **kwargs):
print('我是装饰器')
return func
return inner

@wrapper
def index():
print('我是被装饰函数')
return None
index()

# 应用场景
– 高阶函数
– 闭包
– 装饰器
– functools.wraps(func)
</code></pre>

 

 

 

 

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.