Python Tutorial: if __name__ == '__main__'

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

A rare example of boilerplate that could and should be excised from Python.

Why not simply:

if __main__:
    do_the_things(42)
👍︎︎ 13 👤︎︎ u/Zomunieo 📅︎︎ Sep 12 2019 🗫︎ replies

TIL something :)

👍︎︎ 2 👤︎︎ u/robeastbme 📅︎︎ Sep 12 2019 🗫︎ replies

Huh, I always just make sure that stuff I want to import doesn't have any code that runs, just functions, and put tests elsewhere.

👍︎︎ 2 👤︎︎ u/Al3xR3ads 📅︎︎ Sep 12 2019 🗫︎ replies
Captions
virtually every tutorial series out there includes this bit of code early on and rightly so as it's important and its use is almost ubiquitous in PI files as a beginner it looks complicated in reality there's not much to it at all and over the next five minutes I'm going to take you through if name equals main it'll boost your core understanding of Python and how it works tenfold to start off we've defined a function get handle in it we ask for your Twitter handle and whatever the user types in we assign to a variable we'll call handle we've also made handle this functions return value that means that when we then code something like H equals gets handle whatever the user then types in will be assigned to H let's make another variable call it length and we'll have it equal to whatever the length of H is l en is a built-in function of pythons that does this to finish off we'll print this to the screen let's run it perfect the length of Google is six this functions pretty handy I've got a different program I want to write that could do with get handle but I don't want to have to type that function definition again of course the answer is to import it in Python any file with code in it any file with the extension dot PI is called a module but we refer to it with just the name and forget the dot Pi extension just now we wrote code in a file called gets handled hi therefore we wrote a module called gets handle importing a module means that Python will run that modules code but crucially makes it then available to us albeit we have to tell Python we're accessing that other modules code by typing the module name first and then a dot this is also known as a namespace in this second program we'll get someone's user name then print it all in uppercase letters running our new code we see a problem whilst we wanted the function available to us we didn't actually want the rest of the get handled program to run as well looking at our original program we can place an if statement now when we run our program directly from the terminal we run all of this code but if we import it from another program we have the functions available to us without any of the other code running as well how does this work Python before any of your code is run sets several special variables one of these is double underscore name double underscore this pattern of leading double underscores and then trailing double underscores is commonly called dunder for short so double underscore name double under school is called dunder name less of a mouthful I know let's open the interpreter we can see that dunder name is a string and its value is dunder main I can import get handle just like in our second program when we kept to see what that modules dunder name value is it's get handle the module that's opened and run directly for instance from the terminal here has a dunder name value of dunder main if that very same module is imported then its dunder name value is its module name that means that we can put code that we want executed if our program is run directly in the body all if Thunderer name equals thunder main the result is that the functions we've defined will be available if we import that module from another program but the rest of the code that we want to be specific to if our module is run from the terminal won't be executed libraries which are meant to be imported and used will have some if name equals main code which are actually unit tests that means that you can run the library module from the terminal and it will run tests this is very handy for the developers you don't want those tests run when you're merely importing the library for simple use of one of its defined functions so in summary we've learnt that Python defines special variables even before your code is executed one of these is double underscore name double underscore if the Python file which we call a module is run directly ie from the terminal then Python assigns the string double underscore main double underscore to double underscore name double underscore on the other hand if the same module is imported from another module then it's done dad name value will be the module name which is simply the file name without the dot spy extension I hope you've enjoyed this novice tutorial on the if name equals main statement if you've enjoyed this then please consider liking and subscribing to help support the channel I look forward to your feedback and your requests in the comments below
Info
Channel: undefined
Views: 5,535
Rating: 4.8277512 out of 5
Keywords: if name == main, if __name__ == '__main__' python, python 3 tutorial, python tutorial, python 3, python, __name__ == __main__ python, __name__ == __main__
Id: Mauq4qCgsOk
Channel Id: undefined
Length: 5min 15sec (315 seconds)
Published: Thu Sep 12 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.