In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what the essence of the decorator in python is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Many people make the decorator very complicated, but the essence is very simple.
First of all, what is a decorator? It is found in the code that the one wearing the @ xxx hat is the decorator.
So how do you define your own decorator?
In fact, any callable that receives a parameter can be used as a decorator, such as functions and classes. For convenience, the following examples are illustrated by functions.
Def deco (func): return 1
Here, deco can be used as a decorator.
@ decodef f (args): pass
"wearing a hat" is actually a call. Whoever wears the hat (a function definition) indicates who will be called as an argument and then assigned to a variable with the same name.
The above example is equivalent to f = deco (f). The result is that the function f becomes 1.
Of course, we don't use decorators to return 1. Our main purpose is to maintain the original function and add additional functions.
Then we define a decorator that takes a function as an argument and returns a function.
Def deco (func): return func
In this way, we can add code to enhance the functionality before return func, but what if we still need to tamper with the func? Also need to capture the parameters of func args tamper with it? So we use another function to wrap it.
Thanks to the fact that functions are first-class citizens, we can define functions in functions. This is the most commonly used definition of decorator, which is as follows
Def deco (func): def newfunc (* args, * * kwargs): func (* args, * * kwargs) return newfunc
* args, * * kwargs are used to capture parameters.
To output information before and after the function execution, all we need to do is
Def deco (func): def newfunc (* args, * * kwargs): print 'before' ret = func (* args, * * kwargs) print' after' return ret return newfunc
After understanding the essence, the messy "decorator without parameters" / "decorator with parameters" / "function decorator" / "class decorator" / "multiple decorators" / "Why @ route can automatically collect url" and so on are all appearances.
A decorator with parameters?
You just call it as a function.
About what the essence of the decorator in python is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.