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

Комментарии • 17

  • @Indently
    @Indently Месяц назад +16

    Let’s goooooooo

  • @ryaneakins7269
    @ryaneakins7269 Месяц назад +8

    I had no idea `supress` existed. I made my own, and called it `ignored`. I guess it's a quick enough refactor...

  • @MaxShapira2real
    @MaxShapira2real Месяц назад

    Awesome stuff! 🚀

  • @spectrapulse2104
    @spectrapulse2104 Месяц назад +2

    ah yes I was waiting for just this topic

  • @JohnMitchellCalif
    @JohnMitchellCalif Месяц назад +1

    wow, that's really useful! Subscribed

  • @collegeinvestor7095
    @collegeinvestor7095 Месяц назад

    The terminal info of what branch and py version is neat, have you done a video on that?

    • @Carberra
      @Carberra  Месяц назад

      I have! It's in the description.

  • @JorgeLuis-ts6qp
    @JorgeLuis-ts6qp Месяц назад

    Es una gem tan hidden que está descrita en la documentación.

  • @user-jg6dv2be2x
    @user-jg6dv2be2x Месяц назад

    This theme looks cool, what is it name?

    • @Carberra
      @Carberra  Месяц назад

      I did a video on it linked in the description 😄

  • @Daniel_Zhu_a6f
    @Daniel_Zhu_a6f Месяц назад +1

    nobody would have needed this stuff, would they just expose a local `goto` or make a `defer` keyword.

  • @birdie123
    @birdie123 Месяц назад +4

    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.

    • @vasiliigulevich9202
      @vasiliigulevich9202 Месяц назад

      Do you want to say "traditional way of coding context managers"?