MENU

Fun & Interesting

Python's contextlib is a HIDDEN GEM

Carberra 13,016 11 months ago
Video Not Working? Fix It Now

The purpose of contextlib is fairly evident by the name (unless you've never heard of context managers), but it has some really cool tools that I wanted to share! Plus, there's a really neat decorator trick you can achieve with it too! 00:00 - Intro 00:32 - Using @contextmanager to create context managers [1] 06:46 - Using closing to close contexts [2] 08:26 - Using suppress to suppress exceptions [3] 10:47 - Using redirect_stdout and redirect_stderr [4-5] 14:24 - Outro [1] https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager [2] https://docs.python.org/3/library/contextlib.html#contextlib.closing [3] https://docs.python.org/3/library/contextlib.html#contextlib.suppress [4] https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout [5] https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stderr — If you want to see more of me, join my Discord server! https://discord.gg/5vVgQpK I get a lot of people asking, so here's my setup! • Visual Studio Code: https://youtu.be/PBQspYkkksU • Terminal: https://youtu.be/4RuGK3w6Mbs • Desk/recording gear: https://kit.co/Carberra • PC build: https://uk.pcpartpicker.com/list/TmzGYN — If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so. #python #coding #howto

Comment