Python for the .NET developer 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.
0:02 It's called with and the syntax
0:04 is super super similar. with.
0:06 Create the item as the item name or variable.
0:10 Just use with. It's really really nice.
0:12 It's like Try, and then finally enclose
0:15 or dispose but it's even better than C#'s.
0:18 Why is it better?
0:20 Well with C#'s, using statements
0:22 using a thing and then that thing in the end
0:25 even if there's an exception
0:26 it gets disposed called on it, right?
0:29 IDisposable, that sort of thing.
0:31 With Python's version
0:32 when you implement it behind the scenes
0:34 like you do not see it happen here
0:36 but if you created a class
0:37 that was able to be used in this context
0:39 the finally closed part calls the close
0:43 the right time
0:44 even if there's an error
0:45 but it also passed to that class that's being used
0:48 in the context manager
0:50 whether or not it ended in success.
0:52 So when it closes it
0:54 or it calls the finally disposed type of thing
0:57 it passes, here's the error, here's the exception
0:59 or it passes None, null
1:01 for that value and you know whether or not
1:05 when you're disposing it
1:06 whether it's success or false.
1:08 Imagine IDisposable took a nullable exception type
1:12 that was part of that when
1:13 that when that function when disposed got called.
1:15 That's what Python is like
1:17 and it's really nice.
1:19 Super cool, you can implement a ton of fun stuff.
1:21 This and plug it into these with statements.