When Should You Use the Name-Main Idiom?
00:00 Now that you know about what the name-main idiom does and how it works, does that mean you should add it to all of your scripts? Well, not necessarily. But first, let’s look at when you would want to use the name-main idiom.
00:14 Generally speaking, you want to use the name-main idiom when you have a file that’s going to be run as both a stand-alone script and an importable module, but you also have to have some code that you don’t want to run when it’s being imported. Said another way, you want to avoid producing side effects when importing. In practice, the most common times when you need to actually use the name-main idiom are if you have a module that contains some useful functionality, and you want to create an entry point to it from the command line, or if you have a script, and then you want to write tests for it that would involve importing some content into a separate test file while not running the script’s main functionality.
01:11 It’s a collection of useful math functions, and if you wanted to access some of its functionality directly from the command line, you could use the name-main idiom to collect some user input to control what you want to run, and then call the appropriate functions based on the input you’ve received.
So looking at the ways in which you can collect user input, you already have seen the
input() function, which prompts a user for input into the console, but you can also provide inputs directly into the command line. When you’re running your script, after the filename, you can add multiple arguments separated by spaces. In this example, there are two arguments,
WORLD, and those would be passed into
Note that if you tried running this command with our current
repeat.py, it would ignore those arguments. But in a moment, you’ll see how to update
repeat.py to access those arguments first through the
argv variable from the built-in
These arguments will be used to get the text to repeat from the user, and instead of doing it through the
input() function. In addition to
sys.argv, if you needed to accept more complicated arguments, you could instead use a dedicated command-line interface library to handle more complex inputs. You’re not going to see an example of that in this course, but I will provide some additional resources for you to learn more about creating command-line interfaces at the end of the course.
Then on the line that is accepting the text through user input, instead of calling the
input() function, you can access
argv is a variable that contains a list of arguments passed into the script. And all of those arguments are going to be strings.
03:29 Then the first argument is always going to be the filename. So, we actually want to ignore that one. You can get the argument from one onwards to get all of the ones that are passed into the script. Then,
you can join all of those together with spaces in between by calling the
.join() method on a string that includes the space character (
" "), and that will take all of the arguments passed into your script and combine them into one single line of text with spaces.
So imagine you originally had the
repeat() function, and that’s being imported by other scripts. Now, by using the name-main idiom here, you’ve got an entry point to the function through the command line and a dedicated place to work with user inputs that won’t affect any other scripts.
Here’s a new file called
test.py that shows how to test your
repeat() function using the
unittest framework that comes included in the Python standard library. In the first line, you’re importing the
unittest package, and on the next line, you’re importing the
repeat function from the
Note that this line will only work as is if
repeat.py are in the same folder. This is also the line that will execute the
repeat.py file because it’s being imported as a module. Remember, in
repeat.py, you’re using the name-main idiom and only asking the user for input or accepting arguments when the file is run as a script.
So that way, if you’re importing it like here in the test, you’re not actually going to run that additional functionality, and you can just import the function on its own. So, so after the import statement, you are creating a new class, which is a test case, and it has one method that checks if the default number of repetitions for the
repeat() function is equal to two.
And then at the end, it’s also using the name-main idiom, and it’s running the test suite when this file is called directly as a script, and you can see how it runs. If you do
python test.py, it says it ran one test, and everything was correct and successful.
Become a Member to join the conversation.