Python's contextlib is a HIDDEN GEM
HTML-код
- Опубликовано: 31 июл 2024
- 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] docs.python.org/3/library/con...
[2] docs.python.org/3/library/con...
[3] docs.python.org/3/library/con...
[4] docs.python.org/3/library/con...
[5] docs.python.org/3/library/con...
-
If you want to see more of me, join my Discord server!
/ discord
I get a lot of people asking, so here's my setup!
• Visual Studio Code: • My Visual Studio Code ...
• Terminal: • Make your terminal loo...
• Desk/recording gear: kit.co/Carberra
• PC build: 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
Let’s goooooooo
oooooooooooooooo
:o
I had no idea `supress` existed. I made my own, and called it `ignored`. I guess it's a quick enough refactor...
Awesome stuff! 🚀
ah yes I was waiting for just this topic
wow, that's really useful! Subscribed
Thank you!
The terminal info of what branch and py version is neat, have you done a video on that?
I have! It's in the description.
Es una gem tan hidden que está descrita en la documentación.
This theme looks cool, what is it name?
I did a video on it linked in the description 😄
nobody would have needed this stuff, would they just expose a local `goto` or make a `defer` keyword.
The "contextmanager" module is by far the most useful amongst the others.
It saves a lot of duck-typing compared to the traditional way of coding decorators.
Do you want to say "traditional way of coding context managers"?