在Python中使用参数的装饰器
在本教程中,我们将讨论Python中带有参数的装饰器,但在开始这个主题之前,用户必须学习 在Python中的装饰器 ,函数装饰器。
装饰器是Python中非常强大和有用的工具,因为它允许用户修改函数或类的行为。
Python函数可以被视为最高级的对象。
- 函数可以被引用为一个变量。
- 函数可以作为参数传递给其他函数。
- 函数可以从函数中返回。
带有参数的装饰器与普通装饰器类似。
语法:
@decorator(params)
def function_name():
'''Function implementation'''
带参数的装饰器的代码实现
def function_name():
'''Function implementation'''
function_name = (decorator(params))(function_name)
"""
当我们执行这段代码时,执行将从左到右开始,调用 decorator(params) 来返回函数对象 func_obj 。函数对象 func_obj 将使用 func_obj(function_name) 调用。在内部函数中,将执行所需的操作,并返回用于分配给 function_name 的实际函数引用。现在,用户可以使用 function_name() 调用已应用装饰器的函数。
如何使用带参数的装饰器
首先,我们将看到如果在不实现任何值的情况下直接运行参数代码,我们可以获得什么输出。
def decorators_1(*args, **kwargs):
def inner_1(func_1):
'''
doing operations with func_1
'''
return func_1
return inner_1 # this is the function_object mentioned in the above content
@decorators_1(params)
def func_1():
"""
function implementation
"""
在上述代码中,由于params为空,我们可能会遇到一些错误。
让我们逐步理解这一步骤:
def decorator_function(function_name):
print("Inside the Decorator: ")
def inner_1(*args, **kwargs):
print("Inside the Inner Function: ")
print("'Decorated the function'")
# perform this operations with function_name
function_name()
return inner_1
@decorator_function
def function_to():
print("Inside the actual function")
function_to()
输出:
Inside the Decorator:
Inside the Inner Function:
'Decorated the function'
Inside the actual function
代码执行的可视表示
装饰器执行内部:
内部函数执行:
装饰器函数执行:
最终输出执行:
在上面的代码中,我们会从使用带有参数的装饰器调用的函数中获取输出。
替代方法
在以下代码中,我们将看到如何以另一种方式编写使用函数装饰器的代码。
def decorator_fun(function_name):
print ("Inside the Decorator: ")
def inner_1(*args, **kwargs):
print ("Inside the Inner Function: ")
print ("'Decorated the function'")
# Perform this operations with function_name
function_name()
return inner_1
def function_to():
print ("Inside the actual function")
# This is another way of using decorators
decorator_fun(function_to)()
输出:
Inside the decorator
Inside the inner function
Decorated the function
Inside the actual function
现在我们将看到使用修饰符的不同示例,带有参数以便更好地理解概念。
示例1:
def decorator_1(*args, **kwargs):
print("Inside the Decorator")
def inner_1(function_1):
# Here, we will see the functionality of the code:
print ("Inside the inner function")
print ("I am studying ", kwargs['JTP'])
function_1()
# Returning the inner function
return inner_1
@decorator_1(JTP = "COMPUTER SCIENCE AND ENGINEERING ")
def my_function():
print ("Inside the actual function")
输出:
Inside the Decorator
Inside the inner function
I am studying COMPUTER SCIENCE AND ENGINEERING
Inside the actual function
代码执行的可视化表示
最终输出执行:
示例2
def decorator_function(A, B):
def Inner_1(function_1):
def wrapper_1(*args, **kwargs):
print ("I am studying COMPUTER SCIENCE AND ENGINEERING ")
print ("Summation of values - {}".format(A + B) )
function_1(*args, **kwargs)
return wrapper_1
return Inner_1
# here, we are not using decorator
def my_function(*args):
for ele in args:
print (ele)
# another way of using decorators
decorator_function(22, 14)(my_function)('Computer', 'Science', 'and', 'Engineering')
输出:
I am studying COMPUTER SCIENCE AND ENGINEERING
Summation of values - 36
Computer
Science
and
Engineering
上面的示例还表明,内部函数可以访问外部函数的参数。
代码执行的视觉表示
示例3:
def deco_decorator(dataType, message_1, message_2):
def decorator_1(function_1):
print (message_1)
def wrapper_1(*args, **kwargs):
print(message_2)
if all([type(arg) == dataType for arg in args]):
return function_1(*args, **kwargs)
return "Invalid Input"
return wrapper_1
return decorator_1
@deco_decorator(str, "Decorator for 'string_Join'", "stringJoin process started ...")
def string_Join(*args):
st1 = ''
for K in args:
st1 += K
return st1
@deco_decorator(int, "Decorator for 'summation_1'\n", "summation process started ...")
def summation_1(*args):
summ1 = 0
for arg in args:
summ1 += arg
return summ1
print (string_Join("I ", 'am ', "studying ", 'Computer ', "Science ", "and ", "Engineering"))
print ()
print ("The sum is equal to: ", summation_1(22, 12, 48, 133, 627, 181, 219))
输出:
Decorator for 'string_Join'
Decorator for 'summation_1'
stringJoin process started ...
I am studying Computer Science and Engineering
summation process started ...
The sum is equal to: 1242
流程的可视化表示
返回 decorator_1:
返回的wrapper_1:
执行 message_1:
执行String_Join:
执行 summation_1:
最终输出执行:
结论
在本教程中,我们讨论了如何使用带有参数的装饰器来执行函数。我们还使用内部函数和参数的外部函数的可视化表示来解释了示例。