O bloco with e os gerenciadores de contexto | Live de Python #302

Nesta live, vamos explorar os Gerenciadores de Contexto em Python, uma das ferramentas mais poderosas para o gerenciamento de recursos e controle de fluxo. Vamos entender como o bloco with funciona, o protocolo por trás dele (com os métodos __enter__ e __exit__), e como podemos usar a biblioteca contextlib para simplificar nossa vida. Também veremos como o gerenciamento assíncrono entra em cena com o async with, além de exemplos práticos e boas práticas para aplicar em seus projetos.

-------------------

O canal é mantido por uma iniciativa de financiamento coletivo:

Apoia-se: https://apoia.se/livedepython
picpay: @dunossauro
Chave pix: pix.dunossauro@gmail.com
Meus contatos e redes: http://dunossauro.com/

-------------------

Código e Slides: https://github.com/dunossauro/live-de-python
Telegram da live: https://t.me/livepython
Temas das próximas lives: https://github.com/dunossauro/live-de-python/issues/568
Lista das lives anteriores: https://github.com/dunossauro/live-de-python/blob/main/codigo/README.md

--------------------

# Referencias

[1] “3. Data model - Asynchronous Context Managers”, Python documentation. Acesso em: 30 de outubro de 2025. [Online]. Disponível em: https://docs.python.org/3/reference/datamodel.html#asynchronous-context-managers
[2] “3. Data model - With Statement Context Managers”, Python documentation. Acesso em: 30 de outubro de 2025. [Online]. Disponível em: https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers
[3] “8. Compound statements”, Python documentation. Acesso em: 30 de outubro de 2025. [Online]. Disponível em: https://docs.python.org/3/reference/compound_stmts.html
[4] “10. Full Grammar specification”, Python documentation. Acesso em: 2 de novembro de 2025. [Online]. Disponível em: https://docs.python.org/3/reference/grammar.html
[5] “Built-in Types”, Python documentation. Acesso em: 30 de outubro de 2025. [Online]. Disponível em: https://docs.python.org/3/library/stdtypes.html
[6] “contextlib — Utilities for with-statement contexts”, Python documentation. Acesso em: 30 de outubro de 2025. [Online]. Disponível em: https://docs.python.org/3/library/contextlib.html
[7] “Contexto”, Michaelis On-Line. Acesso em: 31 de outubro de 2025. [Online]. Disponível em: https://michaelis.uol.com.br/moderno-portugues/busca/portugues-brasileiro/contexto/
[8] “PEP 343 – The ‘with’ Statement | peps.python.org”, Python Enhancement Proposals (PEPs). Acesso em: 31 de outubro de 2025. [Online]. Disponível em: https://peps.python.org/pep-0343/
[9] “PEP 617 – New PEG parser for CPython | peps.python.org”, Python Enhancement Proposals (PEPs). Acesso em: 2 de novembro de 2025. [Online]. Disponível em: https://peps.python.org/pep-0617/
[10] “PEP 806 – Mixed sync/async context managers with precise async marking | peps.python.org”, Python Enhancement Proposals (PEPs). Acesso em: 31 de outubro de 2025. [Online]. Disponível em: https://peps.python.org/pep-0806/
[11] “Defer - Learn Swift By Examples”. Acesso em: 1º de novembro de 2025. [Online]. Disponível em: https://learn-swift.com/defer/
[12] “Defer, Panic, and Recover - The Go Programming Language”. Acesso em: 1º de novembro de 2025. [Online]. Disponível em: https://go.dev/blog/defer-panic-and-recover
[13] “exceptions - Documentation for Ruby 3.5”. Acesso em: 1º de novembro de 2025. [Online]. Disponível em: https://docs.ruby-lang.org/en/master/syntax/exceptions_rdoc.html
[14] dotnet-bot, “IDisposable Interface (System)”. Acesso em: 1º de novembro de 2025. [Online]. Disponível em: https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-9.0
[15] “The try-with-resources Statement (The Java™ Tutorials - Essential Java Classes - Exceptions)”. Acesso em: 1º de novembro de 2025. [Online]. Disponível em: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html Receive SMS online on sms24.me

TubeReader video aggregator is a website that collects and organizes online videos from the YouTube source. Video aggregation is done for different purposes, and TubeReader take different approaches to achieve their purpose.

Our try to collect videos of high quality or interest for visitors to view; the collection may be made by editors or may be based on community votes.

Another method is to base the collection on those videos most viewed, either at the aggregator site or at various popular video hosting sites.

TubeReader site exists to allow users to collect their own sets of videos, for personal use as well as for browsing and viewing by others; TubeReader can develop online communities around video sharing.

Our site allow users to create a personalized video playlist, for personal use as well as for browsing and viewing by others.

@YouTubeReaderBot allows you to subscribe to Youtube channels.

By using @YouTubeReaderBot Bot you agree with YouTube Terms of Service.

Use the @YouTubeReaderBot telegram bot to be the first to be notified when new videos are released on your favorite channels.

Look for new videos or channels and share them with your friends.

You can start using our bot from this video, subscribe now to O bloco with e os gerenciadores de contexto | Live de Python #302

What is YouTube?

YouTube is a free video sharing website that makes it easy to watch online videos. You can even create and upload your own videos to share with others. Originally created in 2005, YouTube is now one of the most popular sites on the Web, with visitors watching around 6 billion hours of video every month.