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/01 Report--
This article mainly introduces "how to use Python to achieve with context manager". In daily operation, I believe many people have doubts about how to use Python to achieve with context manager. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Python to achieve with context manager". Next, please follow the editor to study!
With context manager, which has been used many times, such as linking to the database, getting cursors, executing sql statements, and closing connections when executing mysql statements. Or read and write the text content, turn on the text writing content and turn off the text.
If you have to do so much each time a statement is executed, there will be a lot of repetitive code, and using the context manager can solve this problem beautifully and easily.
The role of the with management context is to simplify some repetitive code and to optimize the way try/except/finally is written.
The context is implemented through two magic functions, enter and exit, and later updated and rebuilt, it can be done more easily using API provided by contextlib.
After the concept and function are understood, the most important thing is how to implement it.
For example: when the prodigal son goes to buy cat cookies, he has to do these actions every time he buys them, take out his wallet, spend x yuan, and take back his wallet. It's troublesome to pick up your wallet every time, and if you forget to pick up your wallet, you can't pay for it, and if you forget to collect it, you lose it. You have to write a code every time you take out your wallet. Is there any way to introduce it?
Use enter and exit to implement (1) class wallet (object):
Def _ init__ (self,man):
Self.man=man
Def _ enter__ (self):
Print (self.man + 'take out your wallet boldly')
Def _ _ exit__ (self, exc_type, exc_val, exc_tb):
Print (self.man + 'put your wallet away carefully')
Def use_money (man):
Return wallet (man)
With use_money ('langzi') as a:
Print ('spent 600RMB')
Return the result:
Langzi took out his wallet boldly.
It cost 600 yuan
Langzi carefully put away the wallet using enter and exit (2)
Of course, you can also write:
Class wallet (object):
Def _ init__ (self,man):
Self.man=man
Def _ enter__ (self):
Print (self.man + 'take out your wallet boldly')
Return self
# return self is a very important step to return an instance
Def _ _ exit__ (self, exc_type, exc_val, exc_tb):
Print (self.man + 'put your wallet away carefully')
Def use_money (self,money):
Print (self.man + 'spent' + money + 'yuan')
With wallet ('prodigal son') as a:
A.use_money ('600')
Running result:
The prodigal son took out his wallet boldly.
The prodigal son spent 600 yuan
The prodigal son carefully puts away his wallet and uses contextlib to realize import contextlib.
@ contextlib.contextmanager
Def use_money (man):
Try:
Print (man + 'take out your wallet boldly with great confidence')
Yield None
# yield generator, running here will return a value (you can write any one you want)
Finally:
Print (man + 'put away your wallet very carefully')
With use_money ('langzi') as a:
Print ('spent 1 yuan')
Return the result:
Langzi was very relieved and took out his wallet boldly.
It cost 1 yuan
Langzi put away his wallet very carefully, and the study on "how to implement with context Manager with Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.