What Does the Name-Main Idiom Do?
00:00
Let’s start by looking at what if __name__ == "__main__"
is actually doing. To demonstrate, here’s an example program that doesn’t use if __name__ == "__main__"
in it. In the file repeat.py
, you’ve got a repeat()
function that accepts some text and a number and returns that text repeated that number of times with newline characters in between so that they print on separate lines.
00:31
At the bottom of the file, it asks the user to input some text, and then it calls the repeat()
function with that text and the default number of repetitions, which is 2
, and then it prints it out.
00:45
Now there are two ways you could use this file. First, you could run it as a script, and second, you could import it as a module. Let’s use repeat.py
both ways and see what happens.
01:00
To run repeat.py
as a script in the command line, you would just use the python
command then the filename, and press Enter.
01:10
Here on the left, you can see our code for repeat.py
, and on the right we’ve got our console or terminal. To run this file as a script, we can use the python
command and then the script name, which is repeat.py
.
01:27
Now it’s asking for some text. I can type in Hello, world!
and then it repeats that with the default number of repetitions, which is 2
. Great, so that’s what we expect.
01:43
Next, to import repeat.py
as a module, you would add an import
statement to your file or your Python console. So let’s try importing it in a Python console.
01:55
I’m going to go back to the terminal and use the python
command to open up the Python console or Python REPL. We can do from repeat
, which is the file, import repeat
, which is the function. Okay, and it’s asking us for some text. That’s a bit odd.
02:19
And can we still use that repeat()
function?
02:28
Yep, that works. So we can still import that repeat()
function, but there’s a bit of a side effect happening here, which is that it’s asking us for some text, and that’s not what we expect when we’re just trying to import a function into our console.
02:48 This is happening because even if you’re just importing one function, the whole file is executed first, and then the definitions are imported in. Let’s look at another example where you’re importing into another file.
03:04
Here’s a file called lyrics.py
, and at the top, you’ve got an import statement that says from repeat import repeat
. Note that in order for this import statement to work as written, lyrics.py
must be in the same folder as repeat.py
.
03:21
So after the first line that imports the repeat()
function from the repeat
module, it prints the lyrics for the song “Around the World” by Daft Punk, calling the repeat()
function with the text of "Around the world"
and 144
repetitions, and then it prints it out. So in the terminal, if we ran lyrics.py
, python lyrics.py
, it’s now asking us for some text.
03:50 Once we press Enter, the rest of the file runs as expected. So that’s not what we want. When we just look at this file, it doesn’t look like we should be having to type anything in first before the file runs. So how do we fix this?
04:11
Let’s try adding if __name__ == "__main__"
to repeat.py
. Here, we’re back in the repeat.py
file, and just after the repeat()
function, we are going to add if __name__ == "__main__"
as a string and then indent the next two lines and save it.
04:40
So going back to the terminal, we can run the file again as python repeat.py
, and it works just like we would expect. But now if we were to run lyrics.py
,
04:58 we’re no longer asking for any text input, and it runs as expected. And if we were to import it in the Python console,
05:10
from repeat import repeat
, now it’s not prompting us for text anymore. Great,
05:21
it worked. So what is if __name__ == "__main__"
actually doing? Well, it allows you to specify some code that only executes when the file is run as a script, and it doesn’t execute when the file is imported as a module.
05:38 Now there’s no official name for this line of code, but as stated in the overview, you can call it the name-main idiom. And if you look at this page in the Python documentation, you can see why.
05:52
Here’s the documentation where this line of code is referenced, and it’s talking about the "__main__"
name. And if you scroll down to Idiomatic Usage, you can actually see where if __name__ == "__main__"
is being referenced in the documentation.
06:13 So that’s why we call it the name-main idiom, at least in this course.
06:21
Now that we know what the name-main idiom does, why is it useful? Well, for one, it allows you to avoid side effects when importing files as modules, like we saw when repeat.py
was asking for some text input.
06:36
And this is the case when you’re importing into the Python REPL or into another file. Or if you already have a module, it allows you to add an entry point to that file so that you can use a function in it, like repeat()
, right from the command line.
06:56 And now that we’ve seen what the name-main idiom does, let’s look at how it works.
Become a Member to join the conversation.