Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Comparing a io.StringIO Object With a File

00:00 Alright, so here we are in a new Python REPL session and I’m going to start off by importing io because that’s the module that we’re working with.

00:08 And now I want to show you the difference between the file-like object that you get when you use open(), and then the StringIO object that you’ve seen in the previous lesson.

00:18 So let’s start by calling this one a real_file because the object is built from opening a real file that lives somewhere on your disk. We had one called example.txt and opened this one in read mode.

00:33 Alright, so this real file now is an instance of TextIOWrapper that lives in the io module. This is what you always get when you open a text file using for example, the open() function.

00:47 And then you also got to know something that only lives in memory. And I’m going to call that one fake_file

00:55 and you created it by saying io.StringIO() and then passing some text in there.

01:02 I’ll just say Hello, World!. Alright. And this fake file is a StringIO object that lives at a certain memory location. Now you’ve seen in the previous lesson that both the TextIOWrapper and the StringIO object have a lot of things in common.

01:21 You can read both of them, you can seek with them, you can also write to both of them. So what’s the reason that they share so many methods? And the reason is that they both inherit from the same parent class, which is io. TextIOBase.

01:39 That’s the class that both of these classes inherit from. And let’s confirm that by using isinstance() and passing it first, our real_file and then io.TextIOBase.

01:59 And I see that pattern returns True, and if I do isinstance() on the fake_file io.TextIOBase, then I also get True.

02:10 So both of them inherit from this io.TextIOBase. And this is why they share so many methods and why you can do almost the same things with both of these objects.

02:21 Now the differences are somewhat negligible, but if you’re curious to see what the differences are, you can play around with using dir() and then creating a set out of that, maybe we call that string_io_methods.

02:41 And then we have the,

02:49 I will do set(string_io_methods) minus a set of the text_io_methods. And then you can see that setstate() and getvalue() are two names that only exist for StringIO objects.

03:06 And if you do the opposite,

03:13 then you’ll see which ones only exist for the TextIOWrapper object.

03:20 But most of them are the same so if you look at string_io_methods, you can see that there’s a bunch more methods that exist on these objects, and most of them are the same between both of them because they inherit from the same parent class.

03:35 Okay, and that’s just a short dive into the differences between the two. It’s not that important for practicality. You can basically assume that you can do with a StringIO object what you can also do with a TextIOWrapper object, so the one that you get from opening a real file.

03:53 And that gives you a couple of opportunities that you can use in testing.

Become a Member to join the conversation.