 Welcome back everyone. This is Brian. In this video, we're going to talk about the underscore. The underscore is often ignored, has multiple uses and just gets downright confusing. One of the hurdles of learning, not just Python, but any language is the syntax. And Python syntax is a bit different than other languages. So it often leaves people, even the seasoned programmers a bit mystified as to what these things are. For example, you could have a single underscore, you could have a double underscore, it could be before it could be after, or it could be a combination of before and after. But each one of these different methods has, well, a slightly different reason for being and we're going to dive in and take a look. The first use case we're going to do is a single and it's going to be skipping. Now, when I was a young lad, I used to skip school and it just did not do me any favors. But in this case, skipping is actually a positive thing or say for x in range. And I just want to print out hello. And I want to print out hello, say like five times or something like that. So I'm just going to put a number in here. Now, do you notice a fundamental problem? x, it's not used. The code runs just fine. But if you run this through any sort of streamlining program or any sort of interpreter that's going to try and tell you how to make your program faster or better, it's going to start complaining that x is not used. So what we can do is just simply replace x with a single underscore. Go ahead and clear our program out. And you'll see that it runs exactly as expected. Basically, by using an underscore as a variable, you're saying, I don't care. I just need a variable. And I'm not ever going to use it. Python, make a variable and then get rid of it. That's really what we're saying. So in a sense, we are now skipping the whole variable process. Now, to really dive into the complexity of the underscore, we need to make a test class that we can play around with here. So I'm going to go in here, say person.py. And let's go ahead and say class person. And for right now, I'm just going to say pass. So all we're doing is just simply creating that class. And then we're going to go ahead and import it. Or say from person import star. Just know that we're going to be flipping back and forth between our script in our class, because I want to really demonstrate a lot of the usage of underscore, and it's not really apparent why it's needed unless you're working inside of a class. First thing we're going to really look at here is called the weak private. And it is the single before if you scroll up, this is what it looks like right here. And it's for internal use only, it's called a weak private because we're playing around with scope. And this is why I made a class as we're about to see here. So I'm going to get rid of this pass. Let me say weak private. So when you get the image in your mind of a weak private, what are we talking about here? What we're talking about scope is as far as internal or external. If something's private, it's internal to this class. If it's public, it's external to the class. A weak private means that it's internal to this class. But that safeguard is weak, meaning you can access it outside of the class, even though you really shouldn't. Let's go ahead and demonstrate here. So I'm going to say underscore name equals no name. What have we done here? We've made a variable called underscore name equal no name. That single underscore denotes that it is a weak private. We're telling the world that, hey, we want to use this internally. We don't really want the outside world playing around with it. However, we can demonstrate why this is a weak private because we can actually play around with this. We want it to. Now I would highly encourage you not to play around with your privates. But if you really feel inclined, just, you know, make sure you keep that business to yourself. And we're going to say self dot underscore name equals name. Fairly obvious what we're doing there. We're just saying the self name this guy is going to equal that argument. Now we can go ahead and print this out and just verify that it did actually change. Let's slip back here and let's see it in action. So I'm going to say P equal person P and we want set name Brian. Go ahead and run this. See what happens. Sure enough, name set to Brian. So it's working as expected. Now, oftentimes you'll get asked if you go variable dot and your intelligence pops up, you see all these things with underscores and there is underscore name. Interesting. So really now what we can do is we can grab this guy and do things with it, even though we really, really shouldn't. Let's demonstrate. So I'm going to say weak private. And let's go ahead and change this to P name. So now we can actually read the weak private. And even though we really shouldn't discuss a no, even though we really shouldn't, we can access and modify that weak private. I put no because you should never, ever, ever do that. If you see an underscore in it, do not try to modify it, even though Python will let you do it. Bad things could happen that underscore that single underscore denotes its internal use only we say internal internal to its scope in this case, the class. So yes, whoever made this class is nice enough to let you access it. You really shouldn't. You shouldn't rely on that. Instead, you should rely on functions to get and set values. Okay, let's just take a step back. And let's think about scope for a minute. We've talked about internal use only weak privates. But now we're going to talk about the double. And it's before this is internal use only and it avoids conflict in a subclass and tells Python to rewrite the name. It's also called mangling. So what are we really talking about here? Well, we're talking about a variable that starts with two slashes in front of it. What we're doing is we're telling Python, we want it for internal use only and take another extra step and change the variable name automatically. Ooh, that sounds kind of scary. So let's go ahead and figure out what we need to do here. So let's jump into our class. And I'm going to make a note here so I don't get lost. Strong private, this is what we're talking about here. You want to have a strong private all joking aside, let's take a look here. So I'm gonna say def. And I want underscore underscore think. So seems pretty simple, actually, when you think about it. I'm going to print thinking to myself. I mean, no one can really read your thoughts. And if you could, well, we'd all probably be in a lot of trouble. We're gonna say def and we're going to make another function called work. And I want to say self underscore think. And because we're in the same scope, everything works as expected. But now let's introduce another class child. And we've talked about inheritance before. So this should be nothing new. Just going to inherit the person class. And we're going to say def, test, double, just need a name. I don't really care what it is. And we're going to say self underscore and oh, notice how even though we're inheriting from person, it's not seeing underscore underscore think. It's just got that. Well, let's let's try to force it. So we're going to grab this whole thing. And let's see what's going to happen there. Let's go ahead and save our class and jump back to our script file here. And I'm going to make a new instance here. So I'm gonna say p good person. Could have reused the other other instance, but we're just going to do a whole new one. I'm gonna say p dot work. And let's watch this actually work. So thinking to myself, I can call it as expected. Now what we're going to do is try to do the same thing we did with a week where we're going to go in here, we're say p dot underscore underscore think and notice how IntelliSense is even going to help us out here. We're going to just try and run that and oh, has no attribute. What's going on here is we've now made that a strong private and mangling is happening in the background for subclasses. So we cannot even access that function is just gone. There's just no way to even see it from the outside world, but we can see it perfectly fine in our internal class. Oh boy. Alright, so let's just comment that out and let's play around with us a little bit further here. So I'm going to say C equals child. Remember child actually inherits from the person. And in the child class, we have a test double which is going to call self underscore underscore think which is actually a part of the person. Where are you? There you are. This is where mangling is really going to be demonstrated here. So I'm going to say C and I want to say test double. Any guesses what's about to happen here? Let's go ahead and clear this save run and oh, child object has no attribute child think so that's really what's going on here is we're saying that it's only allowed in that class in which it's declared. Pretty much. There's millions of uses for it. But really what you want to know is that if you want to make something private and to that class and only that class you would do the double underscore. I'm going to go ahead and comment these out so we can move on. Let's go ahead and demonstrate a really cool feature. We're gonna talk about after any. Now what are we talking about after any right here? Any number of slashes after really there is a good use for this. Let's say I want to make a class object and I want to call it person. Does anybody see the problem with this before I run it? Raise your hand you in the back. Okay, class. You guessed it invalid syntax because class is a key word. Oh, that is frustrating. I really wanted to name this class. Well, this is where we can do something like this. And really what we've done is we've created an entirely new name, but we can still call it class. It just prettifies it is that even a word prettifies it beautifies it. So we can use the word class, but we can still not have a naming collision with any of the keywords. And that's really what this is designed to do. Is it helps avoid naming conflicts with keywords. And then from here, we can just say something like print class. And then we can print it out work with it as we want to say, made an object and everything's going to work as we would expect. Wrap this whole thing up. We're going to talk about before and after and we've used this before. And this is considered special to Python for functions like init and main and things like that. We've seen it before. But I want to kind of go into our class here and look at how we could actually use it. So let's go here. We're gonna say before and after. And of course, we've done the init. If you skip that video, basically, this is the constructor. But you notice the syntax here, we've got double before and double after. So basically what we're telling Python is this is going to be internal to the class and we want to avoid naming conflicts. And at the same time, it will not mangle it for subclasses, but each subclass will have its own instance. That's how we can have a constructor for person and a constructor for child. We covered that in our video about the initializer. So let's go ahead and make our own function using before and after. I'm gonna say def all. It would have been really cool if I would have like spelled it right the first time. There we go. And I'm going to just say print. Awesome one. So it looks pretty simple. And it's actually in the spirit of Python, it is dead simple. And it actually works as expected. So let's go ahead and say p equals person e dot underscore underscore. And you notice there's a lot of stuff in here that starts with underscore underscore. I'm gonna say call. And we're just calling a function here. Save, run, and call someone. Ta da, it works. Now, side note, I would not recommend starting any function that you want to be accessed outside of this class with an underscore simply because the underscore starts with a weak private, which you're basically saying to the outside world, hey, this is going to be internal to the class. And it gets stronger from there where you can actually start mangling it, or you can tell you know, everyone, hey, internal, don't want you to use it, but also don't mangle it. That's the before and after. When in doubt, double underscore is your friend before and after. If you do it with the mangling effect for strong private, you may start having some weird issues. So always keep that in mind. But the general rule of thumb is anything private is going to start with an underscore and do not access anything private in a different scope, even though we've demonstrated how to do it, it is not the best way of doing things. You should use a getter and a setter, which we've demonstrated like right here, set name. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there, drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become the more I'll create and publish out on YouTube. Thank you for watching.