Python Context Managers and the "with" Statement (__enter__ & __exit__)

Описание к видео Python Context Managers and the "with" Statement (__enter__ & __exit__)

https://dbader.org/python-tricks ► Discover Python's advanced features and how to use them to your advantage

The "with" statement in Python is regarded as an obscure feature by some. But when you peek behind the scenes of the underlying Context Manager protocol you'll see there's little "magic" involved.

So what's the `with` statement good for? It helps simplify some common resource management patterns by abstracting their functionality and allowing them to be factored out and reused.

In turn this helps you write more expressive code and makes it easier to avoid resource leaks in your programs.

In this Python programming tutorial you'll see how you can add support for the "with" statement to your own objects either by implementing the "__enter__" and "__exit__" protocol or by using the contextlib @contextmanager decorator.

You can use the approach demonstrated in the video to add support for the "open as" design pattern in your own Python classes to follow a more Pythonic style of OOP.

To get more Python Tricks and to discover the full potential of Python check out "Python Tricks: The Book" at the link below.

FREE COURSE – "5 Thoughts on Mastering Python" https://dbader.org/python-mastery

PYTHON TRICKS: THE BOOK https://dbader.org/pytricks-book

SUBSCRIBE TO THIS CHANNEL: https://dbader.org/youtube

* * *

► Python Developer MUGS, T-SHIRTS & MORE: https://nerdlettering.com

FREE Python Tutorials & News:
» Python Tutorials: https://dbader.org
» Python News on Twitter:   / @dbader_org  
» Weekly Tips for Pythonistas: https://dbader.org/newsletter
» Subscribe to this channel: https://dbader.org/youtube

Комментарии

Информация по комментариям в разработке