Home  >  Article  >  Backend Development  >  Python programming: Re-understanding of decorator functions

Python programming: Re-understanding of decorator functions

王林
王林forward
2023-05-27 15:57:221124browse

Decorators are very useful tools in Python. A decorator is a function that takes another function as a parameter and extends its functionality without explicitly modifying it. It allows us to modify the behavior of a function or class without touching its source code.

In other words, a decorator wraps a function to extend its behavior rather than permanently modifying it.

Starting from this article, let’s study what decorators are and how they work in Python.

Python programming: Re-understanding of decorator functions

1.1 About functions

In order to understand how decorators work, we need to review some important concepts about functions in Python. Always be aware that in Python, functions are first-class citizens, so keep the following concepts in mind:

  • ü Functions can be assigned to regular variables;
  • ü Functions can be passed as parameters to other functions;
  • ü Functions can return functions;
  • ü There can be other functions (internal functions) in the function body.

Let’s take a look at function examples on these points.

1.1.1 Example-1: Assigning value to a regular variable

# 把函数赋值给常规变量:

# 定义简单函数
def sayHi(name:str):
return "Hi " + name + "."

#应用:函数赋值给变量
hi = sayHi
print(hi("Solo Cui"))
#输出结果
Hi Solo Cui.

In the code, we define the function as sayHi. Then assign this function to a local variable named hi. This variable hi is also a function - in this case, the assigned variable can be regarded as an alias for the function. The next step is to call the variable hi as a function: hi("Solo Cui").

1.1.2 Example-2: Function passed as parameter

The code list is as follows:

# 函数作为参数传递
def printHello(name):
print("Hello,", name)

# 把函数作为参数的函数
def hiWithFunction(func, xname):
func(xname)

#调用以函数为参数的函数
hiWithFunction(printHello,'上官婉儿')

The code is easy to understand: two functions are defined , a function can receive functional parameters, and another function is passed as a parameter to a function that can receive functional parameters

1.1.3 Example-3: Function that returns a function

The code list is as follows:

#示例3:返回函数的函数
def returnXFunction():
return sayHi #示例1中定义的函数,可自行定义其它函数

# 调用函数
xHi = returnXFunction()
print(xHi("BirdMan"))

Here defines a simple function that returns another function, that is, returnXFunction(). Call the function and assign the returned value to a variable xHi, and then you can The variable name xHi is used to perform the function of returning the function.

1.1.4 Example-4: Function built-in function body

That is, defining a function inside a function body. Please look at the code:

# 示例4:函数体内不含税
def outerXFunction(msg):
'''外部函数'''
#代码...
#定义内嵌函数
def innerXFunc():
'''内部函数'''
print(msg,'来自内嵌函数.')

#函数体内调用内嵌函数
innerXFunc()

#调用外部函数
outerXFunction("火麒麟")

I defined an innerXFunc function inside the function outerXFunction, and called the embedded function inside the external function body, so that when the external function is called, the embedded function is executed.

As shown in the code, when calling the external function, pass the string "Fire Qilin" to the parameter msg. The output "Fire Qilin comes from the embedded function." is completed by the self-embedded function. It should be noted here that the msg variable used by innerXFunc is not defined in its own function body. In other words, it uses the variable from its parent scope - this is the concept of closure in Python.

In a nutshell, about Python closures: A closure is a function object that remembers the value in the scope of the parent object and can be used to create an association between a function and a set of "private" variables. relation. These private variables maintain their persistence across multiple calls to a given function. ​

The above is the detailed content of Python programming: Re-understanding of decorator functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete