在本教程中,我们将讨论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

代码执行的可视化表示:

装饰器内部执行:

341-1.png

内部函数内部执行:

341-2.png

装饰器函数执行:

341-3.png

最终输出执行:

341-4.png

在上面的代码中,我们将从使用带参数的装饰器调用的函数中获取输出。

替代方式:

在下面的代码中,我们将看到如何以另一种方式编写用于函数的装饰器的代码。

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

代码执行的可视化表示:

341-5.png

最终输出执行:

341-6.png

示例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

上面的示例还显示了内部函数可以访问外部函数的参数。

过程的可视化表示:

341-7.png

返回 decorator_1:

341-8.png

返回 wrapper_1:

341-9.png
341-10.png

执行 message_1:

341-11.png

执行 String_Join:

341-12.png

执行 summation_1:

341-13.png

最终输出执行:

341-14.png

结论

在本教程中,我们讨论了如何使用带参数的装饰器执行函数。我们还解释了使用带参数的装饰器的内部函数和参数的外部函数的示例,并进行了可视化表示。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程