How Does the Name-Main Idiom Work?
00:00 In the previous lesson, we looked at what the name-main idiom does and why it’s useful. Now let’s look at how it actually works and why it looks a little bit odd.
00:12
Let’s look at the different parts of if __name__ == "__main__"
, starting with the if
statement. This is just a regular conditional if
statement.
00:24
Next, we have __name__
, which is what’s known as a dunder variable, and Python uses dunder variables for storing metadata, which is data about the program that it’s running.
00:37
The __name__
variable stores the name of a module when it’s loaded. So we’ll talk about module names in a bit, but first, a side note about dunder: there are many dunders in the Python language, both dunder variables and dunder methods.
00:55
The __name__
one is just one that you’re most likely to encounter first. we call them dunders because they have double underscores at the beginning and the end of the name.
01:07 And Python uses this weird naming convention to mark it as reserved for Python’s uses. They’re intentionally kind of ugly or weird or complex looking because a regular programmer would never want to use this naming convention in their own code.
01:25 It also makes it very obvious about what’s part of the Python language itself and what’s part of a program that someone has written in Python. You also shouldn’t be creating your own dunders because they might conflict with Python’s current dunders or ones that they might use in future Python versions.
01:48
For modules, the __name__
variable is set to either the string of "__main__"
if it’s in the top-level code environment or the module’s name if it’s being imported.
02:03
The top-level code environment could be the REPL session, which is the global scope when you run Python in interactive mode, or the script passed to the Python interpreter as a file argument, which just means if you run python file.py
, then file.py
would be that script where that’s the top-level code environment.
02:31
When you run python repeat.py
as a script, Python sets repeat.py
’s __name__
variable to "__main__"
. Then it checks if __name__
is equal to "__main__"
and that if
statement is True
, so it asks for user input and calls the repeat()
function.
02:53
But now, what happens when you run python lyrics.py
? In lyrics.py
, the first line is from
the repeat
module import
the repeat
function. As you learned in the previous lesson, this will actually execute repeat.py
.
03:12
So now inside of repeat.py
, Python sets the file’s __name__
variable to the string of "repeat"
because it’s not in the top-level code environment, and that’s the name of the module.
03:25
Then it checks if __name__
is equal to "__main__"
, which it’s not. It’s equal to "repeat"
, so that if
statement is False
, and no extra code is executed inside of repeat.py
.
03:39
Then the repeat()
function is imported into lyrics.py
, and the rest of lyrics.py
can get executed.
03:49
Here we have a file called name_main.py
, and we are printing the __name__
variable as well as the type of the __name__
variable.
04:00
Then we’re using the name-main idiom and printing "Got here"
if it was True
. We can run this file as a script by saying python
name_main.py
, and __name__
is "__main__"
, and the class is a string. And it does print out "Got here"
because __name__
was equal to "__main__"
in this case.
04:28
When we ran this file directly as a script, it became the top-level code environment, and __name__
is set to "__main__"
. Now, if we were to import this, let’s see what happens.
04:42
We can import it in the Python console. We can say import name_main
, like that, and now the __name__
is equal to "name_main"
, which is also a string, and "Got here"
did not print out.
05:04
To summarize, the if
is just a regular conditional statement, __name__
is a variable that stores the name of the module, and the string "__main__"
is the value of __name__
when you’re in the top-level code environment.
05:23 This allows some code to run when the file is run directly and not when it’s imported into another file. So
05:35 that’s how the name-main idiom works, and in the next lesson, you’ll see when you should be using it.
Become a Member to join the conversation.