Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Python decorator?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is a Python decorator". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Today, let's talk about the decorator with parameters. In order to simplify the business logic and only implement the requirement of string uppercase conversion, focus on the decorator part:

# Business function

Def my_upper (text):

Value = text.upper ()

Return value

Print (my_upper ("hello")) # HELLO

Now the requirements have changed and the core business remains the same, but you need to wrap a layer of HTM tags on the converted characters. The output is as follows:

HELLO

The easiest way is to modify the logic directly in the function, such as:

Def my_upper (text):

Value = text.upper ()

Return "

"+ value +"

"

You are also notified that the requirements have changed, and you have to set a div on the outside, so you reluctantly go back and modify it:

Def my_upper (text):

Value = text.upper ()

Return "

"+ value +"

"

How to deal with the endless modification of the product?

However, technology ultimately has to serve the business, so let's think of a flexible way to deal with the product. Here, the decorator is a good solution. The end result should be like this:

@ tag ("p")

Def my_upper (text):

Value = text.upper ()

Return value

Print (my_upper ("hello")) #

HELLO

How can it be realized? Start with a simple decorator and implement a decorator without parameters

Def tag (func):

Def wrapper (text):

Value = func (text)

Return "

"+ value +"

"

Return wrapper

@ tag

Def my_upper (text):

Value = text.upper ()

Return value

Call

Print (my_upper ("hello")) #

Hello

@ tag grammatical sugar is equivalent to my_upper = tag (my_upper)

My_upper ("hello") is equivalent to wrapper ("hello"). Although you can't access wrapper directly, you can understand it this way.

Using the decorator, the line of business code has not changed, just add the decorator at the function definition to achieve the same function, so how to specify the output style more flexibly through parameters? Use a decorator with parameters

Decorator with parameters

The decorator with parameters only needs to nest a function in the outermost layer on the basis of the original decorator without parameters, define a parameter in the function, and then reference the parameter in the nested function. As you can see from the picture below, I just changed the name of the function inside, and the rest is the same as the original decorator without parameters. Do you think it's more flexible?

Of course, decorators can decorate not only functions, but also classes.

This is the end of the content of "what is a Python decorator". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report