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 has its own using block? They don't use the keyword using, they use the keyword with
0:08 but it's very, very similar. In fact, even the things that can go into the using block have to implement a certain method or interface type of thing
0:16 much like you have to implement IDisposable in C#. So, let's get started with another program here. This one we're going to use the json library.
0:23 In the C# version we use in Python has a nice built-in json library so we'll just import json and we don't need to add it
0:30 to a requirements file or anything like that. And over here we can create a dictionary we'll call it data.
0:36 And the way you create dictionaries in Python is just use curly braces. This is one of the use cases for it. And we'll have a name
0:49 name is Michael, the language is Python. Okay, super, I want to save this using the json library save the data into a file
0:57 using the json library, super easy. So we're going to create Python's equivalent of using block, we use the with keyword.
1:03 And then we're going to create the thing new up something, but for files you just call an open method, and you pass the file name.
1:12 So it's going to be file.json, I think is what we called it. We want to write to that file and we want the encoding
1:19 to be UTF-8, that's usually a good choice. This creates the object and then we're going to define a variable to work with it
1:27 so I'll say fout, for file output stream. This is like a using statement with thing as variable. And then I can go to json and I can say
1:36 dump to a file, it's kind of annoying. The terminology, I'd like save or write or something but this is the way it goes, you can pass the object
1:44 and the file pointer. So the object is data, the file pointer is fout. We're done, that's it. I'll say print. Save to local file, file.json.
1:56 Ready to see if this works in Python? You can bet it will. It ran, it saved to a local file. I noticed over here we now have this file.
2:05 Beautiful, right, how cool is that? And we could even make it prettier, we could go over here and say, indent equals true.
2:12 Now if we go look at it's slightly more formatted and so on, but that's not really the point.
2:17 The point is we have this same idea as the using statement in IDisposable in C#. This is technically called a context manager.
2:27 If you implement the right interface as we'll talk about later when we get to classes then you can use that item here like this.
2:35 You can also do it without defining a variable. It could be that if we didn't have to actually refer to the variable, this'll be fine as well.
2:46 But because we refer to it, it was like that. The warning here is just that it's quote misspelled. We'll fix that problem.
2:52 Awesome, looks like our file got created here and everything's golden.

Talk Python's Mastodon Michael Kennedy's Mastodon