Learn Python Programming, Third edition by Fabrizio Romano Heinrich Kruger
Author:Fabrizio Romano, Heinrich Kruger
Language: eng
Format: epub
Tags: COM051010 - COMPUTERS / Programming Languages / General, COM051230 - COMPUTERS / Software Development & Engineering / General, COM051360 - COMPUTERS / Programming Languages / Python
Publisher: Packt
Published: 2021-10-27T14:23:32+00:00
The output from running this is very similar to the previous example:
$ python context/generator.py About to enter 'with' context Entering 'with' context 139768531985040 Inside 'with' context 139768531985040 type(e)=<class 'Exception'> e=Exception("Exception inside 'with' context") e.__traceback__=<traceback object at 0x7f1e65a42800> Exiting 'with' context After 'with' context
Most context manager generators have a similar structure to my_context_manager() in this example. They have some setup code, followed by a yield statement inside a try statement. Here, we yielded an arbitrary object, so that you can see that the same object is made available via the as clause of the with statement. It is also quite common to have just a bare yield with no value (in which case None is yielded). In such cases, the as clause of the with statement will typically be omitted.
One very useful feature of generator-based context managers is that they can also be used as function decorators. This means that if the entire body of a function needs to be inside a with statement context, you could save a level of indentation and just decorate the function instead.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Exploring Deepfakes by Bryan Lyon and Matt Tora(7476)
Robo-Advisor with Python by Aki Ranin(7356)
Offensive Shellcode from Scratch by Rishalin Pillay(5969)
Ego Is the Enemy by Ryan Holiday(4900)
Microsoft 365 and SharePoint Online Cookbook by Gaurav Mahajan Sudeep Ghatak Nate Chamberlain Scott Brewster(4746)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4421)
Python for ArcGIS Pro by Silas Toms Bill Parker(4047)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(3751)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3466)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(3265)
Speed Up Your Python with Rust by Maxwell Flitton(3221)
Liar's Poker by Michael Lewis(3197)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(3182)
Extreme DAX by Michiel Rozema & Henk Vlootman(3158)
Agile Security Operations by Hinne Hettema(3110)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(3101)
Essential Cryptography for JavaScript Developers by Alessandro Segala(3073)
Cryptography Algorithms by Massimo Bertaccini(2989)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2970)
