If __name__ == '__main__' for Python Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to be covering what it means to include if name is equal to Main in Python why we should use it and the benefits of using it one of the best ways to demonstrate how it should be used is by creating a module so here we can type in let's say sample module and in here we need to add some functionality so the first piece of functionality I'll add is called subscribe and all it's going to say is print but only if you love python so a simple function that prints a simple line inside this module we also want to test that the function is working so of course we will call it right below it we will right click and we'll type on run and you'll get the output but only if you love python so everything's working inside here we can now use this sample module we can go to Main and we can say import sample module and we don't need the sidebar anymore and we can type in Sample module dot subscribe next let's right click to run this and what you'll notice is that we'll get the output written twice in the console and why is that why is it that we only called it once but we got two outputs and there's actually a very simple explanation for that and that is that when you import a module all the code in the module gets executed which means it executes the function definition and then it also executes the function being called so even if we didn't call this down here it's going to run that print statement no matter what and that's not something we really want we just want the user to be able to use it without having these side effects such as the double print statement so of course one way to do this is to remove the Subscribe and it's going to work as a normal module since we're not calling any function in here but that can become quite annoying and I also want to mention very quick that it is not a solution to import from sample module the Subscribe function because that will still import or execute all the code inside sample module even if we only import subscribe it's still going to execute all the code in a module and that's also something else you should keep into account when you're working with python and that is that just because you import specific functionality from a module doesn't mean you're saving on resources you're still importing or executing all the code in the entire module so this is just to be more concise about what you are importing it does not only import this section here it runs the whole script and gives you back only that functionality so it's still going to run subscribe even if you import from a specific section of the module but going back to if name is equal to main this would be the place to use it and to use it I usually just type in Main in pycharm because it has this Auto completion setup so I can just tap enter and it's going to check that if done name is equal to main it's going to do the following code and you'll also get this small play button here which is very nice and convenient because it also helps us with understanding where we're running the code sometimes when I use the shortcut command plus r I'll be running a different script for example right now if I use command plus r I'm not running the correct script right now I'm running main as you can see all the way down here but I can't rely on this line of code because I'm not going to scroll to the right each time to see which one I'm running so many times I make the mistake to run the script and just run the wrong script right now if I make any changes in here they might show up in main because that's where we're running it but it is not executing the code inside here it's actually just executing it in main every time I click on rerun so having this Arrow can make it much more easy to understand where I'm running the script because all I have to do is tap this Arrow once and it's going to run the code inside here you can even print hello if I want so if we print hello it'll print hello and it will also subscribe so we're running the sample module every time we click on play here now if I go back to main and I do command plus r you'll see it's still going to run sample module so it's not really clear which script we're running when we refresh it and being explicit about which script you're running is much better than being implicit but going back to if name is equal to main what does this mean and why are we using it here well all it's saying essentially is that if the script you are currently running is equal to main then you should use the code inside this if statement so right now any script that you run directly by right clicking will be considered main it doesn't matter what the name is up here main will be main if you run it directly sample module will be main if you run it directly whichever script you run directly will be considered main so the code will only execute if you're directly running the script which means now if we were to import it and use it in main here it's not going to run this code down here and we'll just type in hello again it will not run print hello each time we import the module because we are running it directly in main as you can see we run main it will say but only if you love python without printing hello if we remove that and on indent this you'll see that each time we import sample module into main we're going to get hello printed and we're going to get subscribe duplicated because we called both of these each time we imported it so it's incredibly important to include if name is equal to main when you are creating modules because it does save you that trouble of running code that you don't want to run when a user Imports it and it also gives you a great entry point for knowing where to test your code so if you want to just see what this module does you can run the script and you can also use this to show that it works as a standalone script so maybe a user doesn't even have to import this script to make sure it works they can just see at the bottom that it says if name is equal to main which shows that you can run the script directly as it is without having to import it so maybe they just want to use the functionality of subscribe all they have to do is go down to if name is equal to Main and use that functionality personally I would use this as much as possible whenever I make a script I think it's worth it to use if name is equal to main because it does save me a lot of hassle when it comes to running the wrong script personally I tend to run the wrong script many times and sometimes these scripts might have functionality that is so similar that you wouldn't even notice you're running the wrong script until you have some weird bug that you just can't get rid of so even in main.pi I would have if name is equal to main just for me to be concise with what I'm doing to make sure that what I want to run is actually being run and then I explicitly tap on this run main button each time I switch between files just to ensure that I'm running the correct module and then you can use the shortcut as many times as you want but it has just become a habit for me to TAP first on the Green Arrow and then refresh it as many times as I want and I know a lot of you that come from other programming languages might say this is incredibly ugly why can't it be as simple as other languages that just use main or as simple as Java that uses public static void main that is so much easier to read That's so much simpler I'm sure you guys are saying that and I mean it doesn't take any time to write this it might look funky at first but it is pythonic and it does help maintain backwards compatibility so that's why we use it it doesn't take python any effort to create some sort of main definition that you can insert your code you can even create it yourself if you want you can say death Main and inside there you can just put in the code you want and you can simulate your own main function that's not a problem for python so that's a major complaint I see a lot from python developers and that's that they have to use this if name is equal to main syntax and they find that incredibly ugly for me it doesn't matter it does its job pretty well and to type it in Python you just have to type in main especially in pycharm with auto code completion and it's going to insert it for you it doesn't take longer to read the second you see if name it already sounds like main to me so you can put whatever you want in here you can say hello you can say banana and whatever code you want to put inside there and you can ensure that it's being run and that it is meant to be run by tapping on that green arrow then you can refresh the code each time you need to and it's going to reflect the changes in the correct file so those were some of the main reasons to use if name is equal to Main in your files because it does help you ensure that you're running the the correct file and that that file was actually meant to be run plus it prevents unnecessary code from being run in other modules as I showed you earlier if you have any logic inside your other modules and you don't have if name is equal to main all that logic will be run because it has to evaluate the entire file to actually be able to create that import but do let me know in the comment section down below whether you use if name is equal to main or not or whether you learned something new about it or if I missed some sort of important feature that is associated with it but otherwise with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 17,837
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: hZ9rsHdcxtY
Channel Id: undefined
Length: 9min 29sec (569 seconds)
Published: Mon Mar 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.