In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the working principle of Decorators, a powerful decorator in Python, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
This article mainly introduces decorator (decorator). Before we start to introduce decorator, we should have an idea, that is, in python, functions are objects that can be assigned to variables and passed to other functions and returned from other functions, functions can be defined in other functions, and child functions can capture the local state of the parent function.
Demo1.py
Def F1 ():
Print ("F1")
Def register (func):
Func ()
Register (F1)
The decorator is extended on this basis. Then let's talk about when to use the decorator. The main purpose of the decorator is to extend the function of the target function or class without destroying the function (function) or class (class). For example, logging, computing function or class execution time, permissions, and so on.
If you are interested, you can check AOP (Aspect Oriented Programming) again.
With this decorator, we can extract a large amount of code (parts that have nothing to do with the function itself) and write the code into the decorator (which can be reused) without getting messy.
To put it bluntly, it is possible to add additional features to the current functionality (the point is not to destroy the original code).
For example, today I want to record the logging of F1 (), we might write
(normally, you should use logging as a module, but here you can simply use print instead.)
Def F1 ():
Print ("F1")
Print ("logging-F1 is running")
F1 ()
It doesn't seem like a problem to write this way, but what if you need a record of f2 () f3 () f4 () today? ❓
This requires everyone to write the same code ❓.
Can we pull it out, ❓, and this thing is designed to deal with logging.
Of course the answer is yes.
Def my_logging (func):
Print ('logging-{} is running'.format (func.__name__))
Func ()
Def F1 ():
Print ("F1")
My_logging (F1)
The function is implemented, and it looks good. If there is anything else you need to add logging, you can use my_logging (f2). But there are actually some problems with this method. The problem is that you have to call my_logging every time and pass F1 as a parameter. A better way is to maintain F1 as the main business logic, instead of becoming my_logging as the main business logic, that is to say, the current situation destroys the structure of the original code. So a better way is to use the decorator (we finally got to the protagonist) and look at a simple decorator.
Def my_logging (func):
Def wrapper ():
Print ('logging-{} is running'.format (func.__name__))
Func () # run func () Equivalent run F1 ()
Return wrapper
Def F1 ():
Print ("F1")
F1 = my_logging (F1) # Equivalent-> F1 = wrapper
F1 () # Equivalent-> F1 () = wrapper ()
My_logging is a decorator that wraps the real business logic func in it. It looks as if func is decorated by my_logging, so it is called a decorator as its name implies. In this example, you can add something to both the entry and exit of a function, which is also known as AOP (Aspect Oriented Programming). Next, let's talk about the @ symbol, which you can think of as a grammatical symbol.
Def my_logging (func):
Def wrapper ():
Print ('logging-{} is running'.format (func.__name__))
Func () # run func () Equivalent run F1 ()
Return wrapper
@ my_logging
Def F1 ():
Print ("F1")
F1 ()
With the help of @ syntax, F1 = my_logging (F1) can be omitted and F1 () can be used directly.
After reading the above, do you have any further understanding of how Decorators, a powerful decorator in Python, works? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.