You should put this in all your Python scripts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome i'm james murphy from mCoding  and today we're going to be talking about the   def main if name main idiom in python and why  you should be using it in all of your scripts   first off not every file is a script by a  script i mean a file that you intend to run   not a library so something like this  that just defines a function that   you're supposed to import in another file  is not going to benefit from this idiom   most languages like c++ or java have a kind  of boilerplate setup that you need to write   even to write the most basic function java  is notorious for having its public static   void main string args as one of the first things  that a programmer sees before learning to use it   but in python it's seemingly a lot simpler  all you need to do is say print hello world   but what i hope to convince you of by the end  of this video is that you should really prefer   this def main if name main idiom over just writing  things out in a line okay really quickly if you're   not familiar what is this name variable if you  run the script from your terminal or elsewhere   you'll see that the name variable turns out to  be this double underscore main double underscore   but if you import the file  you see something different   in this case we see the double underscore name is  if name main package dot import me to print name   the difference in behavior allows you to check  whether the file is being run as a script or not   if you see dunder main then it's being run as  a script and if it's anything else then it's   being imported you could just stop there and put  all the statements that you want to execute when   this is run as a script into this if block  but i say you should go one step further and   define it as a main function okay so you  can check whether or not your code is   being run as a script or being imported  but why why would you want to do this   is it just a way to flex on programmers that  don't know the idiom or is there a reason for   actually doing this well yes of course there  are reasons for doing it let's go over them   the first and most important reason actually  has nothing to do with the language itself and   everything to do with how people use it yes you  can use if name main to check whether or not your   code is being run versus imported but this isn't  what it signals to the person reading your code   if name main is used to signal that  this file is a script and you can run it   if i look at a file and i don't see if name main  i assume that i cannot or should not try to run it   it makes me think it's supposed to be a  library or that it's only meant to be imported   nothing about the python language actually  enforces this but compare it to using an   underscore as a variable name if you see  an underscore in someone's code they're   basically saying i'm not going to use this  variable nothing stops them from using it   but if they do use it they're kind of breaking  an unspoken rule in the python community   underscore means i'm not going to use this  variable and if name main means this is a script   don't keep your reader guessing if this is  supposed to be an entry point to your program   just make it explicit many editors like pycharm  use this as a signal you can see the file on   the left has this green arrow which says run  hello but there's no green arrow on the right   that's because pycharm knows the signal if name  main means this is something that i want to run   no if name main means this is probably a library  you're not meant to just run it but why should   you extract main out into a function instead  of just leaving the contents in the if block   here's a script that just defines some function  and then uses it to compute something even if i   put this code inside a name guard everything  that i do here is still in the global scope   in particular this variable i is a global variable  even if i didn't really mean it to be that   suppose i define a function to compute some value  and then later on i use it here i'm just trying to   add up some evaluations of this function based off  of this list but i have accidentally made a typo   i use the variable i here but the  name of the loop variable was n   when i run the code instead of  getting an error for my typo   i silently get the wrong value this is one  of the worst kind of errors that you can have   and this is all just within the same file  imagine if this happened in a different file   if in another file or at the console i do an  import star of the bad script i now have a global   variable i that i didn't know about so if it was  this file that defined the compute val function   this file now has a global variable i and it's  even harder than in the last example to find this   typo also in the case where you didn't have an if  name main at all just importing the script will   actually run it that basically means that it's  impossible to test your code without side effects   also note that there are plenty of cases where  importing your code happens automatically   this script explicitly imports the useful class  from the bad script and then writes it to a   pickle file it's clear here that we're importing  the bad script because we explicitly wrote it   but in this script there's no mention of the  bad script all we're doing is unpickling some   object we don't even know where it came from and  yet when we run this code we see that we did in   fact import the bad script and run all of the code  that was in its body this is because pickle.load   automatically imported the bad script in order  to get the definition of the useful class in the   pickle case it was just a little bit annoying to  have that extra stuff printed out but in this case   we're doing some multi-processing the goal is to  compute the values of this useful function over   some list of inputs multiprocessing pool is going  to start up a bunch of different python processes   that all automatically import this file but  without an if name main you can see how this   starts to get into trouble just importing the  file starts up new processes which then import   the file and so on so you pretty much can't use  multi-processing in this case look what happens and i can't even kill it with  control c i have to kill the terminal   putting this code in an if name main prevents  the rampant process spawning but again without   putting this in a separate function this is  now going to give me a global variable named p   all you have to do to avoid this headache is  just use the idiom it's not that much typing   and in fact you could probably just set up  your editor to have it there automatically   when you make a new file or just have a  template to paste it when you type main   one final less used reason  for making a main function   is that this allows you to have an entry point to  your program that you can use from other scripts   so i don't have to run this from the command line  i could import this from another file and then   call the main function as if i was running it from  the command line without starting a new process   in this case you can have your main take in  whatever arguments as an optional parameter   alright that's all i've got on the def main if  name main idiom let me know if you found any   other reasons to use it also let me know if you're  a hold out what your reasons are for not using it   as usual thanks to my patrons and  to my donors for supporting me   if you liked the video don't forget to subscribe  and hit the like button see you next time you
Info
Channel: mCoding
Views: 208,143
Rating: 4.9447117 out of 5
Keywords: python
Id: g_wlZ9IhbTs
Channel Id: undefined
Length: 7min 31sec (451 seconds)
Published: Sat Jul 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.