我们知道装饰器是 Python 中非常强大且有用的工具,因为它允许程序员修改函数或类的行为。在本文中,我们将通过多个示例来 了解带参数的装饰器。Python 函数是一等公民,这意味着函数可以像对象一样对待。
带参数的装饰器与普通装饰器类似。
@decorator(params) def func_name(): ''' Function implementation'''
上面的代码相当于
def func_name(): ''' Function implementation''' func_name = (decorator(params))(func_name) """
当执行从左到右开始时,调用装饰器(params) ,它返回一个函数对象**fun_obj。使用 fun_obj进行调用fun_obj(fun_name) 。在内部函数内部,执行所需的操作并返回实际的函数引用,该引用将分配给func_name。现在,func_name()**可用于调用应用了装饰器的函数。
带参数的Decorator是如何实现的
def decorators(*args, **kwargs): def inner(func): ''' do operations with func ''' return func return inner #this is the fun_obj mentioned in the above content @decorators(params) def func(): """ function implementation """
这里的params也可以为空。
首先观察这些:
# Python code to illustrate # Decorators basic in Python def decorator_fun(func): print("Inside decorator") def inner(*args,**kwargs): print("Inside inner function") print("Decorated the function") # do operations with func func() return inner() @decorator_fun def func_to(): print("Inside actual function") func_to
其他方式:
# Python code to illustrate # Decorators with parameters in Python def decorator_fun(func): print("Inside decorator") def inner(*args, **kwargs): print("Inside inner function") print("Decorated the function") func() return inner def func_to(): print("Inside actual function") # another way of using decorators decorator_fun(func_to)()
输出:
Inside decorator Inside inner function Decorated the function Inside actual function
让我们来看另一个例子:
示例#1:
# Python code to illustrate # Decorators with parameters in Python def decorator(*args, **kwargs): print("Inside decorator") def inner(func): # code functionality here print("Inside inner function") print("I like", kwargs['like']) func() # returning inner function return inner @decorator(like = "geeksforgeeks") def my_func(): print("Inside actual function")
Inside decorator Inside inner function I like geeksforgeeks Inside actual function
示例#2:
# Python code to illustrate # Decorators with parameters in Python def decorator_func(x, y): def Inner(func): def wrapper(*args, **kwargs): print("I like Geeksforgeeks") print("Summation of values - {}".format(x+y) ) func(*args, **kwargs) return wrapper return Inner # Not using decorator def my_fun(*args): for ele in args: print(ele) # another way of using decorators decorator_func(12, 15)(my_fun)('Geeks', 'for', 'Geeks')
I like Geeksforgeeks Summation of values - 27 Geeks for Geeks
这个例子还告诉我们,外部函数的参数可以通过封闭的内部函数来访问。
示例#3:
# Python code to illustrate # Decorators with parameters in Python (Multi-level Decorators) def decodecorator(dataType, message1, message2): def decorator(fun): print(message1) def wrapper(*args, **kwargs): print(message2) if all([type(arg) == dataType for arg in args]): return fun(*args, **kwargs) return "Invalid Input" return wrapper return decorator @decodecorator(str, "Decorator for 'stringJoin'", "stringJoin started ...") def stringJoin(*args): st = '' for i in args: st += i return st @decodecorator(int, "Decorator for 'summation'\n", "summation started ...") def summation(*args): summ = 0 for arg in args: summ += arg return summ print(stringJoin("I ", 'like ', "Geeks", 'for', "geeks")) print() print(summation(19, 2, 8, 533, 67, 981, 119))
Decorator for 'stringJoin' Decorator for 'summation' stringJoin started ... I like Geeksforgeeks summation started ... 1729
1. 装饰器内部
2. 函数内部
注意:图像快照是使用 PythonTutor 拍摄的。
原文链接:codingdict.net