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

preview_player
Показать описание

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 Python Tutorials & News:
Рекомендации по теме
Комментарии
Автор

This tutorial helped me, thanks! I couldn't grasp how Python just KNOWS that it should "open" and "close" the file, especially if it just KNOWS how to do these entry and exit functions for any other command that "with" is compatible with. I finally got it. In the file() example, file.open and file.close are versions of the generalized functions of "__enter__" and "__exit__, " which are functions that exist in all sorts of python modules. "with" works by doing the "__enter__" and "__exit__" functions automatically. I feel like all the other tutorials I tried before this could start by saying that single sentence and everyone would say "Ohhhh".

zoeyschlemper
Автор

This is the best god damn video on the "with" statement I have ever seen.

DanielSaldivarSalas
Автор

This saved me a whole line of code, and for that I'm grateful <3

jvsonyt
Автор

really excellent. one of the clearest technical explanations i've seen

kmarchis
Автор

2019 still best explanation on the web.

Anutechtrwebgodz
Автор

Great explanation, with the user defined class and all. Thank you!

EmanuelRamneantu
Автор

I was just wondering about this the other day. Super clear explanation. Thanks!

toastrecon
Автор

17 March 2020 - One word, Amazing and thanks!

produdeyay
Автор

I am now going to write context managed classes for connection objects. Love this!
Note: one can write code to open a file, then read/write a file, but may or may not remember to close the file, and even if they do close the file, it's less likely to be done in a 'finally:' clause, so there is no guarantee it will be closed.
The point is, try/finally wouldn't even get used for open/close (because people can write bad code, and it still seems to 'work'). This is why we have a context manager: to make python play nice with the OS.
Also, I think the contextmanager decorator code communicates what it is better than the class code, even to someone who may not well understand decorators and generators.

Gruuvin
Автор

Wow, I'm really thankful for your explanation!

filosofiadetalhista
Автор

Great man. Like your vdeos so much. Help me a lot

danielmbicalho
Автор

Nicely explained sir. I'm ur new subscriber. Thanks a lot for this vedio.

hardcode
Автор

Randomly I ended up here found this too good. Now I am gonna watch all of these.

narasimhamurthy
Автор

2021 still the best explanation on the web.

joshuarudd
Автор

Hi, thank you very much. Your videos are really helpful. Well explained! Kudos to you.

MrScX
Автор

I enjoy your videos and I will be looking for your book(s).

rthu
Автор

In my case, the program sometimes RANDOMLY outputs the keys with the ASCII table form.

Ex: "\x08" pressed



sometimes it works ok. anyone knows why

landro
Автор

2:58 The important reason for calling close() (or flush()) on an *output* file is so that you catch any I/O errors on the writes. Otherwise you could suffer loss of data without realizing it!

Consider this littie script:

f = open("/dev/full", "w")
f.write("junk")

That should raise “OSError: [Errno 28] No space left on device”, but because the script exits before the file is properly closed, you never see that. This version, on the other hand

with open("/dev/full", "w") as f :
f.write("junk")
#end with

does properly raise the error. As does

f = open("/dev/full", "w")
f.write("junk")
f.close()

but the with-statement version guarantees to flush the output even if some exception occurs before getting to the end.

lawrencedoliveiro
Автор

Very interesting! I got a little confused on the last example though xD
Is not the decorator already closing the file, making the f.close() inside managed_file function a little redundant?

sowellmemo
Автор

Really helpful. Thanks for the video. One question. Is there any performance loss in this approach ?

kcvinu