Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Python with statements

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Would it surprise you to know that Python
0:02 has its own using block?
0:04 They don't use the keyword using, they use the keyword with
0:07 but it's very, very similar.
0:09 In fact, even the things that can go into the using block
0:11 have to implement a certain method
0:13 or interface type of thing
0:15 much like you have to implement IDisposable in C#.
0:17 So, let's get started with another program here.
0:20 This one we're going to use the json library.
0:22 In the C# version we use in json.net
0:25 Python has a nice built-in json library
0:27 so we'll just import json and we don't need to add it
0:29 to a requirements file or anything like that.
0:32 And over here we can create a dictionary
0:34 we'll call it data.
0:35 And the way you create dictionaries in Python
0:37 is just use curly braces.
0:38 This is one of the use cases for it.
0:40 And we'll have a name
0:48 name is Michael, the language is Python.
0:51 Okay, super, I want to save this using the json library
0:54 save the data into a file
0:56 using the json library, super easy.
0:57 So we're going to create Python's equivalent
0:59 of using block, we use the with keyword.
1:02 And then we're going to create the thing
1:05 new up something, but for files
1:07 you just call an open method, and you pass the file name.
1:11 So it's going to be file.json, I think is what we called it.
1:14 We want to write to that file and we want the encoding
1:18 to be UTF-8, that's usually a good choice.
1:22 This creates the object and then we're going to define
1:25 a variable to work with it
1:26 so I'll say fout, for file output stream.
1:29 This is like a using statement with thing as variable.
1:33 And then I can go to json and I can say
1:35 dump to a file, it's kind of annoying.
1:39 The terminology, I'd like save or write or something
1:41 but this is the way it goes, you can pass the object
1:43 and the file pointer.
1:44 So the object is data, the file pointer is fout.
1:47 We're done, that's it. I'll say print.
1:50 Save to local file, file.json.
1:55 Ready to see if this works in Python?
1:57 You can bet it will.
1:59 It ran, it saved to a local file.
2:00 I noticed over here we now have this file.
2:04 Beautiful, right, how cool is that?
2:06 And we could even make it prettier, we could go over here
2:08 and say, indent equals true.
2:11 Now if we go look at it's slightly more formatted
2:14 and so on, but that's not really the point.
2:16 The point is we have this same idea as the using statement
2:21 in IDisposable in C#.
2:23 This is technically called a context manager.
2:26 If you implement the right interface
2:28 as we'll talk about later when we get to classes
2:31 then you can use that item here like this.
2:34 You can also do it without defining a variable.
2:39 It could be that if we didn't have to actually refer
2:42 to the variable, this'll be fine as well.
2:45 But because we refer to it, it was like that.
2:47 The warning here is just that it's quote misspelled.
2:50 We'll fix that problem.
2:51 Awesome, looks like our file got created here
2:54 and everything's golden.