What is Python's Main Function Useful For?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's we're going to talk about the python main function and now you might be a little bit confused because why do we need a main function in python in python i can just go ahead and write some statements so print hello world for example i don't need to have the classical main function of def main and then a string with the arguments and then arc c or something like we know it from c or java or c sharp or c plus whatever i don't need a classical main function in python to be able to run the code i don't need that entry point so why are we even talking about the main function in python now chances are that you have seen something like that if underscore underscore name underscore underscore equals and then underscore underscore main underscore on the score now this is not really a function but as you can see pycharm already notices that this is special and provides this run button here so what is the function of this line of code why do we need it why is it used so often why is it considered best practice in order to explain that let me create another file here and we're going to call this my library dot py and in here we're going to just define some basic functions so we're going to define an add function uh which returns x plus y we're going to define a sub function which returns x and y or actually x minus y and we're going to define a hello function which is going to do nothing special it's just going to print hello world like that now if i want to use these functions of course i need to import the library into the main file so what i can do is i can just say import my library since they're in the same directory and it's going to be able to do that and now if i want to call the functions i just say my library dot hello or print my library dot at and then 80 and 90 for example now the question is or first of all let me show you that this works there you go the question is how does this actually work how does python do that and python what python does is it basically takes all of that and executes all the lines of code before we get to these lines of code so an import statement is not just picking the functions and saying okay now you can use these functions from that file now it executes the whole python script and then it starts executing the other lines of code so basically this means that if i now have a line down here which is not a function it's just a print statement and i print test down here you're going to see that by just importing this library it's going to execute the code right away there you go you can see test is being being put out onto the screen and this can be a problem because maybe i want to have some lines of code here but i only want to execute them if i run this script directly and i don't want them to be executed when i when i just import the library and in order to see the difference here between uh the context let's just go ahead and print the name variable that we talked about before so we're just going to print name here and we're not going to make the condition here we're just going to print the value and now let's go ahead and run main.py as you can see we get my library as a result here because we import it and then it runs this line of code and in this case the name is my library however if i now go ahead and run my library directly you're going to see that the name changes to main and this is what this condition is for if i now go ahead and write if underscore underscore name equals and then underscore underscore main underscore underscore this code in here is go only going to be executed if i run this module directly so if i say test here and i now run main.py you're going to see that nothing is being executed of course i can still use the functions here but this code is only being executed if i run mylibrary.py directly there you go as you can see so this is basically the reason why we use the main section in python now in addition to that of course it's more readable if we have some code here for example we test the functions or let's just call hello let's just call print at 10 and 20 and then maybe the same with sub let's say i just want to test these functions then of course when i run the library they're going to be tested but if i just want to use the library they're not going to be executed here so this is quite useful and in addition to that what people often like to do or programmers often like to do is they like to define a main function an actual main function with these statements and then [Music] there you go then call the main function in that section so what is the value of that why should we be doing that why should we not just take all these statements and put them down here and the reason for that is because this is more prone to error if we don't do it that way so if we don't use a main function we can run into some errors regarding global variables so let's say we don't have this main function here let's copy these statements again and put them down here let's just get rid of that come on there you go and we need to remove that as well so now if i define a variable down here that we call my value for example and i set it to 10 and then for some reason because i'm confused and i have 50 lines of code in that function i think that my value is defined in hello and hello knows what my value is so if i now go ahead and say print my value i'm not going to get any errors here i'm not going to get any mistakes i don't see that i have a problem because my value is actually defined as a global variable and this can happen across files as well now if i run this of course i'm going to run into an error or actually yeah i'm going to run to an error even if i run the main file because we don't know what myvalue is and if i run my library you're going to see that my value is not defined but i don't see that right away because my value is defined here in the global scope however if i put this into a main function def main and i copy all of this here come on copy there you go and i run the main function now you can see right away that my value is not being recognized in the hello function because of course it is defined locally inside of the main function and this prevents a lot of confusion and stupid errors happening so this is why we use the main function in python by the way of course we can also change this to some lip if we want to and then we're going to have the opposite effect so now if i uh if i run let me just see if i fixed the error i didn't there you go if i run my library.py it's not going to be executed by but if i import it it's going to be executed or actually not somewhat sorry i call this my library this is why it didn't work if i now run my library.py directly we're not going to get any results if i run the main file it's going to trigger the if statement here and we're going to see the results here so this is how we use the main function in python so that's it for today's video if you enjoy hope you'll learn something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 7,237
Rating: 4.9797297 out of 5
Keywords: python, main, function, python main function, main function, if __name___ == '__main__', python name main, python name = main, python if name main, python main name, python if name == main, python main function tutorial, tutorial
Id: lVUOrPunRxQ
Channel Id: undefined
Length: 8min 8sec (488 seconds)
Published: Thu Sep 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.