由于我们必须了解Python函数的基本概念,所以我们应该深入了解与Python函数相关的一些高级概念。在本教程中,我们将讨论Python中高阶函数的重要方面,例如高阶函数是什么,如何在Python中定义它们,如何在Python中使用它们以及高阶函数的属性是什么。

先决条件:

要学习Python中的高阶函数,我们必须对以下概念有基本的了解:

  • Python函数
  • 参数
  • Python对象
  • Python装饰器

首先,让我们从第一件事开始,即高阶函数,了解一些基本概念。

高阶函数

一个将另一个函数作为参数的函数,或者将另一个函数作为输出的返回值的函数被称为高阶函数。高阶函数在程序中与其他函数一起运行。

关于高阶函数的一个事实是,高阶函数适用于接受函数作为参数或返回函数作为其结果的方法,这在Python中的高阶函数的概念中得到了支持。

Python中高阶函数的属性

现在,在本节中,我们将讨论一些适用于Python中的高阶函数的重要属性。

  • 在高阶函数中,我们可以将函数存储在变量中。
  • 在高阶函数中,函数可以充当对象类型的实例。
  • 在高阶函数中,我们可以将一个函数作为另一个函数的结果返回。
  • 在高阶函数中,我们可以将一个函数作为参数传递给另一个函数。
  • 我们可以将Python高阶函数存储在数据结构格式中,例如列表、哈希表等。

Python中的高阶函数

现在,在本节中,我们将专门讨论Python高阶函数以及如何定义它们。我们将讨论定义和使用高阶函数的方法和手段。

以下是在Python代码中定义高阶函数的方法,我们将在本教程中讨论这些方法。

  1. 在高阶函数中使用函数作为对象
  2. 在高阶函数中将函数作为结果返回
  3. 将函数作为另一个函数的参数
  4. 将装饰器作为高阶函数

现在,我们将详细讨论上述每种方法,并学习它们在Python程序中作为高阶函数的实现。

方法1:在高阶函数中使用函数作为对象

在Python中,我们甚至可以将给定的函数分配给一个变量。将函数分配给变量不会调用实际的函数,而是会创建对创建的函数的引用。因此,这使得将函数分配为变量对象的这种分配将在程序中创建一个高阶函数。

看一下下面的示例程序,以了解我们上面讨论的方法的实现:

示例 -

# a default function to take another function parameter  
def spell(text):  
    # Making text in upper  
    return text.upper()   
# Taking text as user input  
text = input("Enter a text to print it in uppercase and double: ")  
# Spell function with text  
print(spell(text))   
# Assigning variable with the default function  
scream = spell  
# Scream with text variable  
print(scream(text))  

输出:

Enter a text to print it in uppercase and double: javatiku
javatiku
javatiku

方法2:将函数作为另一个函数的参数

基本上,Python函数就像Python对象一样,因此我们可以使用Python函数将它们作为参数传递给另一个函数,这将在程序中创建一个高阶函数。

看一下下面的程序,以了解上述方法的实现:

示例 -

# Default function for making text uppercase  
def scream(word):   
    return word.upper()   
# Default function for making text lowercase  
def spell(word):   
    return word.lower()   
# A third function that work as a high order function  
def speak(funct):   
    # Storing the function in a variable in high order function   
    speaking = funct("Hello Python Developers! You are welcomed to javatiku")   
    print(speaking)    
# Printing text in uppercase  
speak(scream)  
# Printing text in lowercase  
speak(spell)  

输出:

HELLO PYTHON DEVELOPERS! YOU ARE WELCOMED TO javatiku
hello python developers! you are welcomed to javatiku

方法3:将函数作为高阶函数的结果返回

我们还可以将一个函数作为另一个函数的结果返回,使该函数成为高阶函数。

看一下下面的示例程序,以了解我们上面讨论的方法的实现:

示例 -

# A default function for addition  
def Adding(a):  
    # Nested function with second number   
    def Addition(b):   
            return a + b # addition of two numbers  
    return Addition # Result  
  
# Taking both number variable as user input  
a = int(input("Enter First Number: "))  
b = int(input("Enter Second Number: "))  
# Assigning nested adding function to a variable  
AddVariable = Adding(a)  
# Using variable as high order function  
Result = AddVariable(b)  
# Printing result  
print("Sum of Two numbers given by you is: ", Result)  

输出:

Enter First Number: 24
Enter Second Number: 26
Sum of Two numbers given by you is:  50

方法4:装饰器作为高阶函数

我们可以将装饰器作为Python中最常用的高阶函数。Python中的装饰器允许我们修改程序中定义的方法或函数的行为,并且它还允许我们将一个函数包装在另一个函数中,以扩展包装或父函数的行为。甚至可以在不永久修改父函数的情况下将一个函数包装在另一个函数中。

在Python装饰器中,一个函数被传递为另一个函数的参数,然后在包装的函数内调用这些装饰器。请查看以下用于在Python程序中定义的装饰器的示例语法。

语法

# Using a decorator  
@JTP_Decorator  
def Python_Decorator():   
    .  
    .  

上述装饰器的语法等同于以下Python代码,用于定义高阶函数的默认Python_Decorator()函数。

# Using Python default function as Python decorators  
def Python_Decorator():   
    .  
    .  
Python_Decorator = @JTP_Decorator(Python_Decorator)  

在上述给定的代码中,我们在默认的Python_Decorator()函数中引用了@JTP_Decorator作为可调用函数。我们只需在这个结构中添加一些额外的代码,我们就会得到包装函数作为输出。

查看以下程序,以了解上述方法的实现:

示例 -

# Using default function as Python decorators  
def Python_Decorator(funct):  
       # Inner nested function  
       def inner():   
              print("This line of code will be printed before the execution of high order function")  
              funct()   
              print("This line of code will be printed after the execution of high order function")  
       return inner   
# A default function as decorator  
def JTP_Decorator():   
    print("This line of code will be printed inside the execution of high order function")  
JTP_Decorator = Python_Decorator(JTP_Decorator) # Python decorator as high order function   
# Python decorator calling out as high order function   
JTP_Decorator()  

输出:

This line of code will be printed before the execution of high order function
This line of code will be printed inside the execution of high order function
This line of code will be printed after the execution of high order function

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