python装饰器的三种形式

一、基本装饰器 (@f)

def f1(h1):
	def f2(args_of_h1):
		print('previous')
		h1(args_of_h1)
		print('after')
	return f2

@f1
def h1(args_of_h1):
	print(args_of_h1)

h1('this is h1')

结果:
previous
this is h1
after

注意看

1、f1的参数是所装饰的函数的函数名(h1)
2、f2的参数是f1所装饰的函数(h1)的参数
3、return f2f1的返回值,返回类型是函数类型

所以说:我执行h1的时候实际上执行的是f2

当我们把 h1(args_of_h1) 注释掉,即

#h1(args_of_h1)

执行结果为:

previous
after

上面的f1具有的缺陷:
1、只能装饰含有一个参数的函数

所以通常装饰器会写成

def f1(h):
    def f2(*args,**kwargs):
        print('previous')
        h(*args,**kwargs)
        print('after')
    return f2
    
@f1
def h1(args_of_h1):
    print(args_of_h1)

(*args,**kwargs)
*表示可以接受多个参数
**表示可以接受多个键值对参数
示例

 ('v1','v2',10,k1='n1',k2=9)

*args将被包装成tuple**kwargs将被包装成dict

二、带参数的装饰器(@f(*arg,**kwargs))

def f(*arg,**kwarg):
	def f1(h):
        def f2(*args,**kwargs):
            print('previous')
            h(*args,**kwargs)
            print('after')
            print(arg)
        return f2
    return f1
    
@f('this is f')
def h1(args_of_h1):
	print(args_of_h1)

结果:
previous
this is h1
after
('this is f',) 

对比一中的代码块,上面代码块增加了一层装饰器使得我可以在@f()中添加参数

注意缩进,我们必须返回一个可调用的函数,这里我们返回f1,f1又返回f2,所以最终执行的是f2最终执行的 f2 始终是 f1 的返回值,f1 以被装饰函数 h 作为参数

三、类包装的装饰器(@app.route(''/))

由上面可知,要使用装饰器装饰一个函数,条件:

1、一个装饰器函数必定返回一个可调用(可使用的函数)
2、只要在一个被装饰函数上使用@decorator(args)即可

那么我的这个decorator函数也可在一个类中

所以app是一个Flask类的对象,route是在Flask类中定义的装饰器函数

下面是一个简单实现,不涉及路由

class Flask():
    def __init__(self):
        pass
    def f2(self,f):
        def f3(*args):
            print('start')
            f()
            print(args[0:])
            print('end')
        return f3

app = Flask()

@app.f2
def hello(*args):
    print("this is hello")

hello('args of hello')

结果:
start
this is hello
('args of hello',)
end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python装饰器是一种高级语法,用于修改、包装或扩展函数或类的行为。装饰器本身是一个函数,可以接受一个函数或类作为参数,并返回一个新的函数或类。下面是几种装饰器的使用方式: 1. 函数装饰器 ```python def my_decorator(func): def wrapper(*args, **kwargs): print("Before the function is called.") result = func(*args, **kwargs) print("After the function is called.") return result return wrapper @my_decorator def say_hello(name): print(f"Hello, {name}!") say_hello("John") ``` 输出: ``` Before the function is called. Hello, John! After the function is called. ``` 2. 类装饰器 ```python class MyDecorator: def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): print("Before the function is called.") result = self.func(*args, **kwargs) print("After the function is called.") return result @MyDecorator def say_hello(name): print(f"Hello, {name}!") say_hello("John") ``` 输出: ``` Before the function is called. Hello, John! After the function is called. ``` 3. 带参数的装饰器 ```python def my_decorator(param): def decorator(func): def wrapper(*args, **kwargs): print(f"Before the function is called with {param}.") result = func(*args, **kwargs) print(f"After the function is called with {param}.") return result return wrapper return decorator @my_decorator("World") def say_hello(name): print(f"Hello, {name}!") say_hello("John") ``` 输出: ``` Before the function is called with World. Hello, John! After the function is called with World. ``` 4. 多个装饰器的组合 ```python def my_decorator1(func): def wrapper(*args, **kwargs): print("Before the function is called by decorator 1.") result = func(*args, **kwargs) print("After the function is called by decorator 1.") return result return wrapper def my_decorator2(func): def wrapper(*args, **kwargs): print("Before the function is called by decorator 2.") result = func(*args, **kwargs) print("After the function is called by decorator 2.") return result return wrapper @my_decorator1 @my_decorator2 def say_hello(name): print(f"Hello, {name}!") say_hello("John") ``` 输出: ``` Before the function is called by decorator 1. Before the function is called by decorator 2. Hello, John! After the function is called by decorator 2. After the function is called by decorator 1. ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值