If __name__ == "__main__" for Python Developers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if name equals main then we run our programs but what does it actually mean here's the simplify take [Music] if you've used python for quite some time you've probably noticed this type of syntax in many cases we include it without understanding what it means or why we even need it which is probably not the best practice so let's quickly explore it from a beginner's friendly perspective so first of all what exactly is name well name is a special variable we use when we interact with modules it allows us to make a distinction between the modules we import and the modules we do not import now so far I was using the term module to describe packages and libraries such as pandas and Pi game and so on however this is not the only definition of a module a module is a self-contained piece of code it is logically separatable from other pieces of code it is so independent that we can copy a module from one program we can then reuse it in another program or we can even execute it all on its own without any other code surrounding it so if that's the case not only libraries are modules but also functions and classes and most of all any python file you have ever created is also considered a module so if we take it back to the name variable we can say that it allows us to make a distinction between a the modules that represent libraries or external functions and classes which we are able to import into our code and B the module that represents the current python file we are executing in our console the file into which we import those functions and classes and libraries now the best way to explain this is with a code example so if you're not sure what I mean so far you will in just a few sec seconds so let's say we have a python file named myprogram.py and it contains a single line of code that just prints this is a module now when we run this file in our console by typing python myprogram.py as soon as we hit enter we are dealing with something called top level code as in the entire content of my program is considered top level which basically represents the entry point into our application what I've previously referred to as the current python file we are executing however what if we need to import some kind of a library so let's say we need pandas in order to create a data frame well in that case the import statement as well as the print statement are still considered top level code however the data frame is not considered top level because we have borrowed a class from a module which we have never specified in our console I mean there is no pandas in Python myprogram.py and that's the exact distinction that the name variable helps us solve it uniquely identifies the top level module and separates it from the rest of the modules and actually that's exactly where main joins the party so unlike name which is a variable main is actually a value it is a string main represents the name of the top level environment in which top level code lives so by checking if name equals main we are actually checking if the code we are executing is top level or not so for example if we add an additional print statement into our file and we pass the global variable of name into it our console will return main perfect however if we try to access the name of pandas we are no longer dealing with a global variable but with an attribute of pandas and in that case our console will return pandas instead ha now what if we import pandas SPD do you think this will affect our name attribute well let's check and no pandas remains pandas regardless of the as keyword we will see shortly why now what happens in cases where our import statement is very specific what if from date time we would like to import the time zone class well in that case the name of time zone is time zone however date time does not have a name at all unless we specify it within an additional import statement and then the name of date time is of course date time so the rule of thumb is we strip away the dot py extension from any python Library which we can find in our operating system and yes date time dot py is an actual file which we can view and we can modify as long as we have python installed now once we strip away the extension we use the name of the file as the name of the module and that's exactly why the as keyword didn't have any effect on our pandas import now if we'd like to fetch a specific class from within this file so for example the date class or the time zone class we simply use the name of the class as the name of the module and voila this basically explains our time zone example so perfect now we know exactly what name and Main means but why do we need to check if they match before we run our programs let's have a look at an example so let's say we have a python file named import me and it contains a function named call me which simply prints a short message now at the very end of this file we also make a call to the call me function because well it politely asked us to so we might as well now when we run this file in our console with python import me dot py we get our print statement and everything works as expected however what if we add another file to the mix so let's say that in addition to importme.py we have runme.py and the only purpose of this new file is to import the call me function from the import me module there is no other need in this file and there's no other code involved now a very interesting thing happens when we execute this file in our console we type python runme.py we hit enter and boom we can see the output of call me in our console once again even though we have never made an official all to this function runme.py simply includes an import statement ha so it seems that simply by importing a function from a module we also accidentally execute this module so the main purpose of if name equals main is to prevent this automatic execution from happening now if we add this condition inside import me dot py right above our function call we can then safely execute runme.py without worrying about this extra print statement just popping out of the blue so we are basically restricting the behavior of call me only to situations in which import me is the top level module in cases in which it's the imported module our call to call me will never manifest and that's exactly why if name equals main is so important now thank you so much for watching if you found this tutorial helpful full please share it with the world and don't forget to give it a huge thumbs up if you'd like to see more videos of this kind you can always subscribe to my channel and turn on the notification Bell if you have anything to say please leave me a comment below with any questions and any requests for future tutorials if you'd like to be extra social you can always join our Discord Community where you can chat with many many talented developers I'm including the link in the description of course and yeah I'll see you very soon in another awesome tutorial in the meanwhile bye-bye
Info
Channel: Python Simplified
Views: 200,714
Rating: undefined out of 5
Keywords:
Id: NB5LGzmSiCs
Channel Id: undefined
Length: 8min 47sec (527 seconds)
Published: Sun Nov 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.