10 Neat Ways To Use Underscore In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video was brought to you by IND dentle IO learning python Made Simple how's it going everyone in today's video we're going to be covering 10 incredibly useful ways that you can use the underscore in Python starting with the first one which I will always regard to as an Easter egg because I never use the python console but in case you do use the python console you can use the underscore in a very interesting way so here I have the python console opened up and I'm going to add 10 + 10 and we're going to get a value such as 20 so what the underscore does in the python console is use the last value so the underscore here is going to be considered 20 so underscore + 10 is going to give us 30 as an output but we can also do something such as create a variable called X and just print X and that's also going to be considered the last value so now we can type in underscore plus let's say Bob and that's because I used a multiplication symbol there so let's try that again underscore plus Bob and it's going to use the last value and append Bob to that value so in the console it can be used for quick maths moving on to use case number two snake case in a lot of programming languages we have different conventions when it comes to naming variables in Java you'd probably use camel case such as car something if you have a class or whatever if it's a variable it might even start with a lowercase and then for each new word you would use an uppercase character sorry about my creativity here it's early in the morning so I'm doing my best but anyway in Python we would not use this convention you might see it around somewhere if someone comes from JavaScript they're probably going to do this in Python but something much more conventional is snake case so car something with an underscore and for a better example we would have something more like long list if we had some sort of long list or if you have a long variable name you would separate those words using an underscore so long V name use case number three when you are dealing with big numbers in Python it can become super easy to lose track of how many zeros you're using or how many digits you're using and in any case that you are required to hardcode this it would be nice to have some sort of indication of how many zeros there actually are so one thing we can do is create a variable called big number of type integer and if you want to create a number with maybe let's say 12 zeros you can use an underscore as a sign for the thousands or it doesn't have to be for the thousands you can actually place it anywhere in your number I think the only Only Rule is that you can't have them directly after each other but otherwise between each number you can put an underscore and if you were to print this big number those underscores will be ignored they're just there as a visual sign for the developer to understand what the number actually is so in Europe you will see numbers being separated by the thousands like this and I find that much more readable than this over here because this requires you to actually read with maybe your Mouse pointer or with your brain or with your eyes whatever this entire number which can be quite difficult if you are looking at it you might actually just have to type in something such as billion and this is not a billion I think this is a trillion I'm really bad with these big numbers but thanks to the underscore I can at least understand how many zeros there actually are up next we have use case number four which is using the underscore for unimportant values for example imagine we have a tuple called stats of type string string and integer which just means that the tupal contains a string a string and an integer and we want to extract values from this tupal but we don't consider all these values to actually be important well one thing we can do of course is unpack it by typing in name underscore for the middle value which we do not care about which is the job and the age cuz all we want to extract is the age and the name well here we can use that underscore to perform the duty of telling the developer that that that value just isn't important and then we can assign stats to it and thanks to this we can print both the name and the age and it will work just fine as you can see name contains Bob and the age contains the age which is 27 it's important to note that the underscore is a valid variable name which means it will contain the value of programmer but conventionally you would not use it for anything it's supposed to be something you ignore because maybe later throughout the program you might assign it a different value it's just there to tell the developer that this value is unimportant use case number five starcore here I'm going to create a variable called values which will be of type list of integer and it will contain five values and once again we want to unpack the values but this time we only want to grab the first value and the last value of this iterable so I'm going to create a tuple or an unpacking operation here using first the variable name of first then I'm going to use starcore for the middle and last at the end then we can type in that's going to equal values and now we can print the first and the last and the separator is going to be set to this underscore so that when we run this what you should notice is that we were able to grab the first variable and the last variable while this absorbed everything in the middle and and if we were to print the underscore you'll see it's going to contain the other values and that's all this does this absorbs everything in between use case number six four Loops in some cases you're not going to care about the variable name of what you're iterating through in a for Loop for example here we can type in 4core in range let's say three and four came out of course we're going to print for loop as you can see we're not using this underscore anywhere inside our for Loop because we don't really care about that value all we care about is that we are iterating through this three times and if we were to run this you'll see that it will print for loop three times otherwise this also works in list comprehensions so let's get rid of that and here we're going to print yo for underscore in range three and the same thing applies here we're not using this value that comes from the range so there's no point in actually giving it a name of course you can change this to I if you want or to whatever you want but we're not going to be using this value so it's not necessary you can explicitly tell the developer that this value does not matter that we just care about iterating through this three times and doing this operation if we were to run this we would get yo yo yo use case number seven semiprivate variables and for this example I'm going to import from uu ID uu id4 and The UU ID type because what I want to do next is create a class called Lamp and all it's going to take inside the initializer is a brand so self. brand is going to equal whatever brand we insert and each lamp is going to contain an ID but we want this information to be exclusive to each one of the classes and its sub classes we don't want the developer to use this information outside of the class so one thing that we can do which is merely a naming convention is use the underscore to Define it as a semi-private variable for example here we can type in self doore ID and that will be of type uu ID which will equal a uu id4 so each time we create a lamp it gives it an ID and then below we'll create a method called get ID which returns a uyu idid and we will return self doore ID so anyone using our program should be using this get method they should never access this directly as we defined in the class otherwise if you also have let's say a subass called subl Lamp this should also be a Fine Place to use that ID as long as it's within the class or the subass for example here we can have something called sub lamp method and that's going to return none but it's going to print let's say self doore ID that should still be fine because we're using it inside the lamp or its subass what's not fine is using it directly outside of the class so if we create an instance of this class and I'm going to do it directly inside the print statement and the class is going to have the brand of Bob we can still refer to the ID outside and it's going to work just fine it's going to grab that ID but if you ever do this outside of a class it should come to you as a huge red flag that it was not supposed to be accessed this way whoever created or designed the class meant for it to only be used internally inside the class or the subass so it's merely a naming convention that tells you that if you happen to see it around in other codebases use case number eight name mangl name mangling name mangling name mangling I'm drinking coffee now use case number eight name mangling once again we're going to be using the same class as from the previous example except this time we're going to use name mangling for our ID so here I'm going to type in self doore hidden ID and that's going to equal a uyu id4 except this time the double underscore is going to give us some special functionality and to demonstrate that I'm going to create a lamp called Pam which is my favorite brand I don't know if it exists or not but if it does it's my favorite brand and what I'm going to do after that is print the brand and if we were to run this it should work just fine we should get the brand of the lamp printed to the console but now what what happens if we try to print the hidden ID so I'm just going to copy that and paste it in here you'll notice that this does not exist we're going to get an attribute error because what we told python with the double underscore is to mangle this name which just means it changed this name slightly at runtime so that it could potenti help with name clashes now just because we mangled this doesn't mean we can't access it nothing is ever going to be private in python as far as I know maybe there will be a new update but as far as I know today there's nothing that you can make private in Python and actually if we were to go back to the console or let's go to number four and number four is the shortcut that takes me to my console just to clear that up if we were to go back here and read the attribute error it's actually going to tell you what we should have referred ref to which is actually the mangled name so as you can see by convention it just puts the class name in front of the Hidden ID variable or whatever the variable is so to access that that's what we need to insert not the double dot though although that would be a cool uh mechanism and if we were to run this it would work this time even if we get a lot of ugly squiggly lines both from mypi that doesn't know this exists and from whatever this is going to be also doesn't know it exists and name mangling is a whole topic on its own M coding has a really good video on this so I'll just search that in on YouTube if you're curious about how to use it properly but to sum this up it just mangles the name at runtime which can potentially help with name collisions up next we have use case number nine Dunder methods and for this example I'm going to import the self type from the typing module and I'm going to create a class called custom number which takes a value and inside the initializer we just assign the value to the instance of the class and then with this I'm going to create two custom numbers and immediately I'm just going to print one of these numbers which is going to be number one and what we should get as an output in the console is this weird custom object at this memory address which isn't that useful in terms of information so what Dunder methods allow us to do is to override built-in functionality for these types for example right now if we were to add these two together we would get an error because we don't have any code defined to handle this operation N1 plus N2 so what we can do is type in Def double uncore add which will take self and other and other preferably is going to be this custom number so I'm going to refer to it as self it can actually be any so it's not restricted to self but I use self because I like to get the context actions from cust number in other and this will return to us an integer and what we're going to do here is create an insane operation in general you should just add the numbers such as self. Val plus other. Val as you can see I'm getting those context actions and return that that would make the most sense for the custom number but I'm going to do this time 1 million and I already messed up that trick so that the next time we actually try to add these numbers together we will get the result of that operation it actually works because we defined that functionality as a Dunder method otherwise let's go back to what we had earlier where I only printed N1 as you can see we get this ugly representation that doesn't tell us anything as a human being or that a human being can understand so what we're going to do instead is remove add because we're not going to use that ever again and we're going to define a representation method which will return to us a string and here we're going to return that the custom number inside parenthesis has a value that equals self. value so that the next time we print our class the representation is going to look like this something that's easy to read otherwise one more example of a dander method can be something such as the or dunda method which allows you to use the pipeline with your custom classes so now we can do N1 n two and when we run this we will get whatever kind of functionality we put inside here and there are a lot of Dunder methods to explore so just type in double underscore method or just double underscore and scroll I'm not getting anything here so def double uncore and scroll moving on to use case number 10 reserved names in some occasions you're going to want to use a name that's reserved by the system such as class or for and you're going to want to assign some sort of value to that whatever that value may be unfortunately you cannot do this because it's a reserved keyword so one convention for reserved keywords is to add an underscore to them so class and four and that just helps you get around that problem and I'm going to change this to Bob as well although of course you can change it to something more descriptive such as classroom if that's what it's supposed to be or four can just be four I don't know present destination although that's an incredibly long name so I really prefer having four there but yeah it helps you use those reserved keywords as variable names and finally call me a dirty liar but I actually included 11 ways to use the underscore in this video so if you feel like dropping a like drop a like if you enjoy this video otherwise don't drop a like I'm not good at saying that I honestly hate asking for likes so do what you want leave a comment say hello to Bob but uh let's move on to number 11 so here we have something called Gangster weather and gangster weather is just the weather in gangster form so in today's weather it's raining and what we're going to be using here is a match statement which is great for pattern matching but you can also use it to simulate an if else Block in some cases if you find it cleaner it wasn't made for that but you can and what we're going to do is cover a few cases so if the gangster weather is equal to the case of rain then we will print that yo find yourself an umbrella otherwise if the case is equal to Sunny we will print yo put on that sunscreen and finally if there's any other kind of weather which we did not cover we can Define that using an underscore as a wild card and that just tells the match statement that if there's anything that's not covered by these two cases we're just going to default to this case so it's just a default block which prints I have no idea yo ask Bob and this block here you can just consider it to be the else Block in an if else statement so if we were to run this you would get yo find your an umbrella as an output because the gangster weather is set to rain but if you type in something such as Thunder which obviously was not covered by any of the cases it's going to default to asking weatherman Bob but yeah those were the 11 most important use cases for underscores in Python do let me know what you think about this video in the comment section down below whether you know of any other use cases which I missed in the video but uh but yeah now I'm actually speaking a lot so as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 19,354
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: -RgtgHvdYP4
Channel Id: undefined
Length: 18min 17sec (1097 seconds)
Published: Thu Jun 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.