In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the use of Decorators, a decorator in Python. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
What are the five characteristics of python? what are the five characteristics of python:
1. It's easy to learn, and when you develop a program, you focus on solving problems, not understanding the language itself.
two。 Object-oriented, compared with other major languages such as C++ and Java, Python implements object-oriented programming in a very powerful and simple way.
3. Portability, Python programs can run on a variety of platforms without modification.
4. Explanation, programs written in Python do not need to be compiled into binary code and can be run directly from the source code.
5. Open source, Python is one of FLOSS (Free / Open Source Software).
Def my_logging (func):
Def wrapper ():
Print ('logging-{} is running'.format (func.__name__))
Func () # run func () Equivalent run F1 ()
Return wrapper
Def bold (func):
Def wrapper ():
Print ("")
Func ()
Print ("")
Return wrapper
Def italic (func):
Def wrapper ():
Print ("")
Func ()
Print ("")
Return wrapper
@ my_logging
@ bold
@ italic
Def F1 ():
Print ("F1")
F1 ()
In fact, the code above is also quite like this.
Def my_logging (func):
Def wrapper ():
Print ('logging-{} is running'.format (func.__name__))
Func () # run func () Equivalent run F1 ()
Return wrapper
Def bold (func):
Def wrapper ():
Print ("")
Func ()
Print ("")
Return wrapper
Def italic (func):
Def wrapper ():
Print ("")
Func ()
Print ("")
Return wrapper
Def F1 ():
Print ("F1")
F1 = my_logging (bold (italic (F1)
F1 ()
I just need to pay a little attention to the order of execution. F1 () just now has no parameters. What if I want to bring parameters today?
Def my_logging (func):
Def wrapper (* args, * * kwargs):
Print ('logging-{} is running'.format (func.__name__))
Func (* args, * * kwargs)
Return wrapper
@ my_logging
Def F1 (* args, * * kwargs):
Print ("F1")
For thing in args:
Print ('hello {}' .format (thing))
For name, value in kwargs.items ():
Print ('{0} = {1} '.format (name, value))
F1 ('twtrubiks', apple='fruit', cabbage='vegetable')
If you use * args * * kwargs, you don't have to worry about whatever parameters F1 () brings. The decorator has more flexibility, such as today if I want to bring the decorator into the parameter.
Def my_logging (level):
Def decorator (func):
Def wrapper (* args, * * kwargs):
If level = "1":
Print ('level {} logging-{} is running'.format (level, func.__name__))
Elif level = "2":
Print ('level {} logging-{} is running'.format (level, func.__name__))
Func (* args, * * kwargs)
Return wrapper
Return decorator
@ my_logging (level= "1")
Def F1 (* args, * * kwargs):
Print ("F1")
For thing in args:
Print ('hello {}' .format (thing))
For name, value in kwargs.items ():
Print ('{0} = {1} '.format (name, value))
F1 ('twtrubiks', apple='fruit', cabbage='vegetable')
The simple explanation here is that in order to bring the decorator into the parameters, we have to wrap it again.
The above is the use of Decorators, the decorator in Python, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.