python 装饰器带参数的

本文详细讲解了如何使用装饰器处理带有参数的函数,包括带参数的装饰函数和无参数函数的装饰。通过实例演示了装饰器如何接受和传递参数,以及不同场景下装饰器的灵活运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

被装饰的函数带有参数

        ​​​​​​​例子1

例子2:

被装饰的函数没有参数

装饰函数带参数


被装饰的函数带有参数

例子1

用来说明代码运行逻辑

def a_decorator_passing_arguments(function_to_decorate):
    def a_wrapper_accepting_arguments(arg1, arg2):
        print("I got args! Look:", arg1, arg2)
        function_to_decorate(arg1, arg2)

    return a_wrapper_accepting_arguments


@a_decorator_passing_arguments
def print_full_name(first_name, last_name):
    print("My name is", first_name, last_name)


print_full_name("Peter", "Venkman")
I got args! Look: Peter Venkman
My name is Peter Venkman

被装饰的函数 print_full_name 有两个参数传进去,头上装饰器 a_decorator_passing_arguments ,它的参数 funciton_to_decorate 代表函数  print_full_name,表示把被装饰的函数传进去;

 a_wrapper_accepting_arguments 才是装饰函数,agr1,agr2 代表被装饰函数的参数 "Peter", "Venkman";

等价写法,也是比较常见的写法,输出一样:

def a_decorator_passing_arguments(function_to_decorate):
    def a_wrapper_accepting_arguments(*args,**kwargs):
        print("I got args! Look:", *args)
        function_to_decorate(*args)

    return a_wrapper_accepting_arguments


# 当你调用装饰器返回的函数式,你就在调用wrapper,而给wrapper的
# 参数传递将会让它把参数传递给要装饰的函数
@a_decorator_passing_arguments
def print_full_name(first_name, last_name):
    print("My name is", first_name, last_name)


print_full_name("Peter", "Venkman")

例子2:

*args, **kwargs 都传入参数:
def a_decorator_passing_arbitrary_arguments(function_to_decorate):
    # The wrapper accepts any arguments
    def a_wrapper_accepting_arbitrary_arguments(*args, **kwargs):
        print("Do I have args?:")
        print(args)
        print(kwargs)
        return function_to_decorate(*args, **kwargs)

    return a_wrapper_accepting_arbitrary_arguments


@a_decorator_passing_arbitrary_arguments
def function_with_named_arguments(a, b, c, platypus):
    print("Do %s, %s and %s like platypus? %s" % (a, b, c, platypus))


function_with_named_arguments("Bill", "Linus", "Steve", platypus="Indeed!")


Do I have args?:
('Bill', 'Linus', 'Steve')
{'platypus': 'Indeed!'}
Do Bill, Linus and Steve like platypus? Indeed!

被装饰的函数没有参数

def a_decorator_passing_arbitrary_arguments(function_to_decorate):
    # The wrapper accepts any arguments
    def a_wrapper_accepting_arbitrary_arguments(*args, **kwargs):
        print("Do I have args?:")
        print(args)
        print(kwargs)
        function_to_decorate(*args, **kwargs)

    return a_wrapper_accepting_arbitrary_arguments


@a_decorator_passing_arbitrary_arguments
def function_with_no_argument():
    print("Python is cool, no argument here.")


function_with_no_argument()
Do I have args?:
()
{}
Python is cool, no argument here.

因为被装饰的函数 function_with_no_argument 没有参数,所以 args,kwargs 为空

装饰函数带参数

def param(test=None):
    def wrapper(func):
        def factor_fun(*args,**kwargs):
            if test:
                print(test,'装饰函数有参数')
            else:
                print(test,'装饰函数 no 参数')
            print(args,kwargs)
            return func(*args,**kwargs)
        return factor_fun
    return wrapper


@param('test')
def zjk(*args,**kwargs):
    print(args,kwargs)


zjk('1','2',name='zjk')
test 装饰函数有参数
('1', '2') {'name': 'zjk'}
('1', '2') {'name': 'zjk'}

以上代码装饰函数带有参数,可以看到装饰函数有两个闭包;

def param(test=None): 代表装饰函数,test 参数代表装饰函数自己的值

def wrapper(func): 代表被装饰函数在这里接收,fun 代表传进来的被装饰函数

def factor_fun(*args,**kwargs): 装饰函数真正的逻辑

所以一般装饰函数带有参数传入,一般都是三个 def 嵌套

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值