What does python decorator mean? How to use python decorators?

PHP中文网
Release: 2017-06-20 14:30:51
Original
1834 people have browsed it

Python---detailed explanation of decorators

Definition:

Essentially a function. Its function is to decorate another function (that is, the decorated function) and add functionality to the decorated function. The premise is that the source code and calling method of the decorated function cannot be changed. Such a function is called a decorator.

Analysis:

## Let’s talk about it below Not much to say, just explain it in code. Below is a function.

      b=1+2
Copy after login

Program output:

————————

3

————————

Now I want to add an explanatory sentence to this function, as follows, we can write a decorator:

 1 #原函数 2 def add(): 3     a=1+2 4     print(a)  
 5 #装饰器 6 def decorator(func): 7     def warpper(): 8         print("1+2的结果是:") 9         func()10     return warpper11 #注意此句   12 add=decorator(add)13 #调用函数14 add()
Copy after login
Program output:

——————————The result of

1+2 is:

3

——————————

In this way we successfully achieved our goal. Pay attention to this sentence on line 12. This sentence passes the add function object into the decorator() function, and returns a new function variable. This new function object is reassigned to add, so that it can be guaranteed not to change. The calling method of the decorated function remains unchanged. There is a more elegant way to replace the statement on line 12 in Python syntax. As follows:

 1  #装饰器 2 def decorator(func): 3     def warpper(): 4         print("1+2的结果是:") 5         func() 6     return warpper 7  8 #add=decorator(add) 9 #原函数10 @decorator#换成@符号11 def add():12     a=1+213     print(a)14 #调用函数15 add()
Copy after login

Add "@xxx" directly in front of the decorated function (xxx is the decorator function name)

#What should I do if the decorated function has parameters?

What if the decorated function has parameters? How should we work? Don't worry, we can collect parameters in the form of indefinite parameters. The example code is as follows:

 1 def decorator(func): 2     def warpper(*args,**kwargs): 3         print("相加的结果是:") 4         func(*args,**kwargs) 5     return warpper 6  7 @decorator 8 def add(x,y): 9     a=x+y10     print(a)11 12 add(2,3)
Copy after login
程序输出:
——————————————————
相加的结果是:
5
——————————————————
Copy after login

Write a page verification decorator below.

We all know that some pages of some websites require users to log in before they can access them, such as the following three functions (representing three pages respectively):

1 def index():2     print("welcome to the index page")3 def home():4     print("welcome to the home page")5 def bbs():6     print("welcome to the bbs page")7     return "I am the return contents"
Copy after login
If we now want to add verification to the home page and bbs page, obviously it is not feasible to change the source code now. At this time we can use the decorator, as follows:

 1 username,passwd="jack","abc123"#模拟一个已登录用户 2 def decorator(func): 3     def warpper(*args,**kwargs): 4         Username=input("Username:").strip() 5         password=input("Password:").strip() 6         if username==Username and passwd==password: 7             print("Authenticate Success!") 8             func(*args,**kwargs) 9         else:10             exit("Username or password is invalid!")11     return warpper12 13 def index():14     print("welcome to the index page")15 @decorator16 def home():17     print("welcome to the home page")18 @decorator19 def bbs():20     print("welcome to the bbs page")21     return "I am the return contents"22 23 index()24 home()25 bbs()
Copy after login

Program result:

————————

welcome to the index page #You can log in directly to the index page without verification

Username:jack
Password:abc123
Authenticate Success! :jack
————————

We noticed that bbs() has a return value. If we change the last sentence of the above code (line 25) to “print(bbs() )" and then look at his output:

————————

welcome to the index page

Username:jack

Password:abc123

Authenticate Success!

welcome to the home page

Username:jack
Password:abc123
Authenticate Success!
welcome to the bbs page
None                                                                                                                        Has it become None? ? ?


————————

What happened! The return value of bbs() is printed as None. how so? Wouldn't this change the source code of the decorated function? How can this be solved? Let’s analyze it:

When we execute the bbs function, it is actually equivalent to executing the wrapper function in the decorator. After careful analysis of the decorator, we found that the wrapper function has no return value, so in order to let it To ensure that the return value of the decorated function can be returned correctly, the decorator needs to be modified:

1 username,passwd="jack","abc123"#模拟一个已登录用户
 2 def decorator(func): 3     def warpper(*args,**kwargs): 4         Username=input("Username:").strip() 5         password=input("Password:").strip() 6         if username==Username and passwd==password: 7             print("Authenticate Success!") 8            return func(*args,**kwargs)#在这里加一个return就行了
 9         else:10             exit("Username or password is invalid!")11     return warpper12 
13 def index():14     print("welcome to the index page")15 @decorator16 def home():17     print("welcome to the home page")18 @decorator19 def bbs():20     print("welcome to the bbs page")21     return "I am the return contents"22 
23 index()24 home()25 bbs()
Copy after login

如图加上第8行的return就可以解决了。下面我们在看看改后的程序输出:

————————

welcome to the index page
Username:jack
Password:abc123
Authenticate Success!
welcome to the home page
Username:jack
Password:abc123
Authenticate Success!
welcome to the bbs page
I am the return contents   #bbs()的返回值得到了正确的返回

——-——————

好了,返回值的问题解决了.

 

既然装饰器是一个函数,那装饰器可以有参数吗?

答案是肯定的。我们同样可以给装饰器加上参数。比如还是上面的三个页面函数作为例子,我们可以根据不同页面的验证方式来给程序不同的验证,而这个验证方式可以以装饰器的参数传入,这样我们就得在装饰器上在嵌套一层函数 了:

 1 username,passwd="jack","abc123"#模拟一个已登录用户 2 def decorator(auth_type): 3     def out_warpper(func): 4         def warpper(*args,**kwargs): 5             Username=input("Username:").strip() 6             password=input("Password:").strip() 7             if auth_type=="local": 8                 if username==Username and passwd==password: 9                     print("Authenticate Success!")10                     return func(*args,**kwargs)11                 else:12                     exit("Username or password is invalid!")13             elif auth_type=="unlocal":14                 print("HERE IS UNLOCAL AUTHENTICATE WAYS")15         return warpper16     return out_warpper17 18 def index():19     print("welcome to the index page")20 @decorator(auth_type="local")21 def home():22     print("welcome to the home page")23 @decorator(auth_type="unlocal")24 def bbs():25     print("welcome to the bbs page")26     return "I am the return contents"27 28 index()29 home()30 bbs()
Copy after login

 

输出:

————————

welcome to the index page
Username:jack
Password:abc123
Authenticate Success!
welcome to the home page
Username:jack
Password:abc123
HERE IS UNLOCAL AUTHENTICATE WAYS

————————

可见,程序分别加入了第2行和第16行和中间的根据auth_type参数的判断的相关内容后, 就解决上述问题了。对于上面的这一个三层嵌套的相关逻辑,大家可以在 pycharm里头加上断点,逐步调试,便可发现其中的道理。 

总结

要想学好迭代器就必须理解一下三条:

1.函数即变量(即函数对象的概念)

2.函数嵌套

3.函数式编程

The above is the detailed content of What does python decorator mean? How to use python decorators?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!