 Hey everybody, it's Brian. Welcome to the 18th Python tutorial. We are going to do something that's going to blow your mind. Are you ready? Serialize objects. Ta-da! What does that mean exactly? Well, we've all done this before, right? Where you've made a class and let's just make a generic class here and we'll say age equals zero. Yeah, we've done this before but I'm just going to flesh this out a little bit here just because name equal unknown. Geez, can't spell. And let's just make a function here. So, when I say we're going to serialize an object, what are you, when what am I talking about here? Are we turning into a serial killer or something or are we making some sort of hybrid robot out of our computer? No, actually, you know, you've made classes before and you've filled in the data and you probably thought, you know what, how do I save this for later? How do I take this class that I'm working with here and actually make it so that I can start to disk and load it later? And that's what we're going to do. And if you've never heard of this before, it's called object serialization. And a lot of programs actually do this. When you're like in a video game and you save your file, that file is just a class or collection of classes and it gets serialized to the disk. The benefit of that is you don't have to write a lot of code to load all this stuff back in. You know, you don't have to, like, you completely ignore the previous videos on binary and text files that we've done. You don't need them. You can just simply serialize the object. You do that by importing something called pickle. Now I have to pause because usually when you import a pickle, that's kind of a weird thing and your friends look at you and there's that awkward moment and then you have to really explain what's going on, why they called it pickle. I have no idea. It's a funny name and it actually cracks me up every time I use it. So we're going to test our object. Now what this will do behind the scenes here, it will make the object and then it will convert all that into everything that you've learned in the previous tutorials about structs and packing and unpacking and reading and writing binary files. It'll do all that for you so you don't have to worry about it. We're just going to flesh out some things here. My name is Brian. I'm 40 years old. Sometimes I don't know if I want to say that but whatever. Wisdom comes with age. So let's test this out. Let's actually make sure our little class here works. Hello, my name is Brian. All right. So it's actually just because I'm picky like that. I want to be able to see all the information. Actually, you know what? No, I don't. I'm going to be less picky. That's my goal for this year is to be less picky. Anyway, so we're just going to leave it like this. Now we're going to write this to a file and test out TXT. I had an interesting email somebody would they know I'm a security not they're like, don't you feel a little weird, you know, reading and writing files and throwing it up on YouTube and having everybody looking at your hard drive and I'm like, not really. I mean, there's really nothing exciting on my hard drive. And this is just my my little play box where I record videos and do nothing else. So anyway, so we're going to open the file and you guessed it, we're going to binary right now. This is where pickle becomes beautiful. You ever seen a beautiful pickle? Well, you're about to pickle is going to take a dump. I know some of you, especially if you're watching this in like a high school classroom are probably laughing really hard, but that's actually what it does. Pickle dumps the data to the desk. So we will dump that out. We're going to print pickle. Let's say the pickle has landed just because I've had one of those days. I need some humor. Now we're going to read that file back in and load it and actually create a new object in memory. This is where your mind is just going to explode. You're going to go, what just happened? So with open file, let's actually do the correct one. We're going to, of course, binary read SF. And we're just going to say, Oh, equal pickle load. Now you need to do a few things here. We need to take our current class that's in memory, that P, and we need to dump it to our file. So what's going on if you're following along here is we have our class called person making an instance of that class in memory and just adding some variables and stuff like that. And I tried not to be picky. We're taking that and dumping it to a file pickles taking a dump. Probably help if I had the right version of dump here. And see it's taken P and it's putting it in F. F is just the file. So don't get confused. P is the instance of our person, F is the file. So it's dumping the person into the file. The pickle has landed will be printed. Now we're making a new variable and pickles going to load that little guy in there. And I actually did that wrong. There we go. Pickles going to load the file in here. Now some some interesting things are going to happen as soon as we do this. Let's actually say print. Oh, just so we can see what's going on. Let's run this. Now hello, my name is Brian. The pickle has landed. Now you see this underscore underscore main person object at what in the heck is all this? Let's actually go out and look at this file. This is our file out on the desk. Now it may look different on your screen depending on what you're looking at it. I'm using g edit because I'm on Linux and this is trying to interpret the hex values. This may come up as a blank document. This may come up as a bunch of weird gobbledygook and smiley faces. It just depends on what you're looking at, right? But this is, you guessed it, a binary file. Now you can see some things in there like you see my name. So you know something's going on here and you see it says name. So you know that it's actually doing things. And there's age. The tricky thing of what this is really doing here is Python is a dynamic language, meaning you don't have to declare a variable type. It knows intrinsically what type of variable it is. It does that by, you guessed it, putting a class type in there. Person, which inherits object at. And then this is the memory address of where this actually lives on my computer. Neat. So let's actually continue with this here. Now if you try to work with that object, like let's say O dot, notice how IntelliSense does nothing for us. That's because PyCharm isn't smart enough to know what type of object that is because it has no idea what's on that disk. So you can do some things and I'm going to actually put this link out here. Because I found this really awesome link. If you're using PyCharm, otherwise just ignore the link. But what it is, it's type hinting in PyCharm. Some simple ways you can get PyCharm to understand what type of information you're working with here. So, and this is all built into Python. So you don't have to worry about learning some proprietary thing. If is instance and you're going to say O and we're going to say the person class, then we're going to print O dot name. Or actually we could say O dot call the function. Watch this. Hello, my name is Brian, none. No idea where that none came from. Let's actually do dot, oopsie. Let's see the age. Make sure the age actually loaded. There it is, 40. Put the name in there. So what you're really seeing here, that's going to kind of bug me, I'm wondering where that none came from. That will be your homework class to tell me where that none came from because I don't have time to look into it right now. I've actually got to rush. But that's what's going on. So you can create your classes, fill them with information, serialize or save them to the disk by making Pickle take a dump. And then later when the computer starts back up and the person starts your program, you can load those back in a memory exactly how they were before. The beautiful part about Pickle is it intrinsically works with all of the Python types. So it knows, it doesn't have to guess, it just knows. That was a pretty awesome tutorial. I'm sorry I fudged a little bit and I'm really wondering why where that none is coming from. That's going to be my homework to figure out what I did wrong here. None. I will figure that out. But if you figure it out before me, post it in my YouTube channel. In the meantime, be sure to visit my website voidrums.com for the source code for this and other tutorials. And I know I've been beating it to death, but be sure to join the Facebook voidrums group. There's 200 of us in there. The reason being, I'm not always available. It can take days, weeks, months to get a hold of me. My inbox is continually slammed. So if you send me an email and you really want some feedback, it's going to take a while. So join the voidrums Facebook group, ask your question, 10 people are going to jump on it and instantly start giving you ideas. So that's it.