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

How to use the decorator of python

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to use the python decorator". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the python decorator.

Definition of decorator

A decorator is a function that adds new functionality to it without changing the code of another function. This is a skill that you must learn to participate in a multiplayer project, and you can't miss the decorator when learning python.

An introduction to decorators

To master the decorator, you have to understand the closure. If you haven't mastered the closure, you can take a look at what I wrote about the closure yesterday. It's easy to learn the decorator after mastering the closure. Today, let's continue the case of yesterday's closure to talk about decorators.

First of all, we have a function to calculate the amount payable and the actual payment when the goods are sold. The code is as follows:

Def count (x, prince, number): # x is the discount ratio, prince is the unit price, number is the quantity result = prince * number # result is payable, equal to prince multiplied by number pay = result * x # pay is actual payment, equal to payment multiplied by x discount ratio print (f 'total price is {result} yuan, paid {pay} yuan')

Now customers have put forward a new requirement to verify the password before running count. The password cannot be executed if the password is wrong, and the password pair can only be executed.

Generally speaking, it is necessary to change the corresponding function to meet the new requirements, but it is easy to cause problems in large projects by changing functions that are not written by yourself.

In python, there is a good way to add functionality to the original function without changing its code. The methods are as follows:

Def checkpwd (func): # def inner (* args, * * kwargs): pwd = input ('Please enter password:') if pwd = "123456": print ("password is correct!") Return func (* args, * * kwargs) # verifies the password before executing the function before the password pair can execute else: print ('password error') return inner@checkpwd # decorator. Function is equivalent to count=checkpwd (count) def count (x, prince, number): result = prince * number pay = result * x print (f 'total price is {result} yuan, actual payment {pay} yuan') count (0.8,2.88,100) out: please enter password: 123456 password is correct! The total price is 288.0 yuan and the actual payment is 230.4 yuan for multiple decorators.

Now the customer has put forward a new requirement, check the discount value before running count, the value range must be between 0. 5 and 1.

Then we need to write another decorator to verify the range of discount values, the code is as follows:

Def checkdisct (func): def inner (* args, * * kwargs): disct = args [0] if disct > = 0.5 and disct = 0.5 and disct

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