Python Context Manager

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2021 May 31 6:59
Editor
Edited
Edited
2024 Mar 17 2:56
Refs
Refs
원하는 타이밍에 정확하게 리소스를 할당하고 제공하는 역할
가장 많이 사용되는 컨텍스트 매니저는 with 문
Python Context Manager Usages
 
 
 
 
The ins and outs of context managers and try-finally in Python
You can't forget! Context managers (used with the "with" statement) and try-finally are both constructs in Python that help you ensure certain cleanup code runs even in the presence of exceptions. Becoming familiar with these constructs is important for developing good coding habits in Python. Don't put the burden of remembering how to do cleanup on yourself, use a context manager to let the object handle its own cleanup! In this video, we take a deep dive into the semantics of context managers and try-finally, including the basic examples, sharp corners (and how to avoid them!), as well as less common use-cases. ― mCoding with James Murphy (https://mcoding.io) Source code: https://github.com/mCodingLLC/VideosSampleCode With statement docs: https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers Try-finally docs: https://docs.python.org/3/reference/compound_stmts.html#finally-clause Pythonizing Imgui video: https://www.youtube.com/watch?v=wh_W-veFpKU SUPPORT ME ⭐ --------------------------------------------------- Sign up on Patreon to get your donor role and early access to videos! https://patreon.com/mCoding Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.) https://www.paypal.com/donate/?hosted_button_id=VJY5SLZ8BJHEE Want to donate crypto? Check out the rest of my supported donations on my website! https://mcoding.io/donate Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, Neel R, Matt R, Johan A, Casey G, Mark M, Mutual Information, Pi BE ACTIVE IN MY COMMUNITY 😄 --------------------------------------------------- Discord: https://discord.gg/Ye9yJtZQuN Github: https://github.com/mCodingLLC/ Reddit: https://www.reddit.com/r/mCoding/ Facebook: https://www.facebook.com/james.mcoding CHAPTERS --------------------------------------------------- 0:00 Intro 0:34 Without context managers... 1:00 Common context managers 2:01 WHEN does cleanup run? 4:36 Try-finally 7:01 With in terms of try-finally 8:11 How to write a context manager 11:38 Suppressing exceptions 12:53 Wrapping an external api 13:54 contextmanager decorator 14:51 "Actual" context managers 15:40 Decimal library 16:21 Limitations 18:51 Thanks
The ins and outs of context managers and try-finally in Python
 
 

Recommendations