


What are decorators in Python? Introduction to decorators in Python
This article brings you what is a decorator in Python? The introduction of decorators in Python has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. What is a decorator?
A decorator, decorator, is essentially a Python function that allows other functions to function without any code changes. To add additional functionality, the return value of the decorator is also a function object.
Decorators enhance the functionality of the core code part without modifying the core code. For example, insert logs, permission verification, transaction processing and other scenarios before functions.
And you can extract a large amount of similar code that has nothing to do with function functions and reuse it.
2. Early implementation: Decorators that are not decorators
A function in Python can also be regarded as an object and can be assigned to a variable , and call the function through the variable.
For example:
def foo(): print("2017-06-08") f = foo # 将函数赋值给变量,f为函数对象 f() # 返回结果: 2017-06-08
3. Syntactic sugar@ Implement a simple decorator
Suppose now you want to enhance foo() Functionality, for example, printing logs before function calls. But I don't want to modify the definition of the foo() function. This way of dynamically adding functionality while the code is running is called a "decorator".
is as follows:
def testfunc(func): print('testfunc') def wrapper(): print("%s %s() called" %(time.ctime(),func.__name__)) return func() return wrapper @testfunc def foo(): print('foo1()') foo() # 相当于执行testfunc(foo) ->wrapper(foo) -> foo()
Execution result:
testfunc Wed Sep 12 15:01:13 2018 foo() called foo1()
Description: Before executing foo(), first execute testfunc(foo), and then Execute wrapper(), calling foo() itself on return.
4. Decorators that receive specific types of parameters
The decorator can receive parameters. When the function returned by the decorator is called, that is The wrapping function is called, passing the parameters into the wrapping function, which passes the parameters to the decorated function.
is as follows:
def testfunc_with_args(func): def wrapper_argus(arg1, arg2): print("I got args! Look:", arg1, arg2) func(arg1, arg2) return wrapper_argus @testfunc_with_args def full_name(first_name, last_name): # 带参数,将参数传给被装饰的函数 print("My name is", first_name, last_name) full_name("Peter", "Venkman") # 调用
Execution result:
I got args! Look: Peter Venkman My name is Peter Venkman
5. Decorator with indefinite parameters
When there are multiple functions that need to call the decorator, but the parameters of the functions are different, how to implement it? There can't be one function corresponding to one decorator. At this time, it can be implemented using a decorator with variable parameters.
is as follows:
def log(func): def wrapper(*args,**kwargs): # 可接受各种参数 print('call %s():'% func.__name__) return func(*args,**kwargs) # 返回一个函数 return wrapper # 装饰器返回一个函数,进入到wrapper() @log # @log放到now()的定义处,相当于执行语句:now = log(now) def now_1(a,b): print('now()1:a+b = ',a+b) @log def now_2(a,b,c): print('now_2():a+b+c = ',a+b+c) now_1(1,2) now_2(1,2,3)
Run it~
6. Multiple decorators
When a function wants to add multiple functions , you can consider using multi-layer decorators, but you need to pay attention to the execution order of the decorators.
Give a chestnut:
# 注意代码的执行顺序 def deco_1(func): print('------111111------') def wrapper(*args,**kwargs): # 包裹函数,参数与原函数的参数一致 print('start: this is deco_1.') func(*args,**kwargs) print('end: deco_1.') return wrapper # 返回值是一个包裹函数 def deco_2(func): print('------222222------') def wrapper(*args,**kwargs): print('start: this is deco_2.') func(*args,**kwargs) print('end: deco_2.') return wrapper @deco_1 @deco_2 def now_1(a,b): print('now()1:a+b = ',a+b) now_1(1,2)
Running results:
# 结果,注意执行顺序: ------222222------ ------111111------ start: this is deco_1. start: this is deco_2. now()1:a+b = 3 end: deco_2. end: deco_1.
7. The decorator itself takes parameters
In addition to bringing parameters to the decorated function Parameters, the decorator itself can also take parameters.
def logging(level): def wrapper(func): def inner_wrapper(*args, **kwargs): print("[{level}]: enter function {func}()".format(level=level, func=func.__name__)) return func(*args, **kwargs) return inner_wrapper return wrapper @logging(level='INFO') def say(something): print("say {}!".format(something)) @logging(level='DEBUG') def do(something): print("do {}...".format(something)) say('hello') do("my work")
Execution result:
# 执行结果: [INFO]: enter function say() say hello! [DEBUG]: enter function do() do my work...
Related recommendations:
Detailed explanation of decorators in python
Detailed explanation of various decorators in Python
The above is the detailed content of What are decorators in Python? Introduction to decorators in Python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.
