Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: Concept: Python with statements
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Python also has a using statement. It's called with and the syntax is super super similar. with. Create the item as the item name or variable.
0:11
Just use with. It's really really nice. It's like Try, and then finally enclose or dispose but it's even better than C#'s. Why is it better?
0:21
Well with C#'s, using statements using a thing and then that thing in the end even if there's an exception it gets disposed called on it, right?
0:30
IDisposable, that sort of thing. With Python's version when you implement it behind the scenes like you do not see it happen here
0:37
but if you created a class that was able to be used in this context the finally closed part calls the close the right time even if there's an error
0:46
but it also passed to that class that's being used in the context manager whether or not it ended in success. So when it closes it
0:55
or it calls the finally disposed type of thing it passes, here's the error, here's the exception or it passes None, null
1:02
for that value and you know whether or not when you're disposing it whether it's success or false. Imagine IDisposable took a nullable exception type
1:13
that was part of that when that when that function when disposed got called. That's what Python is like and it's really nice.
1:20
Super cool, you can implement a ton of fun stuff. This and plug it into these with statements.