11.7. More About
Here is the final copy of our
main() practice program. It runs correctly.
Now let’s mess it up.
random_lists.py code in the editor and scroll to the bottom. Add
a function call to the code:
43 44 45 46 47 48 49 50 51 52 53
def main(): numbers = create_random_number_list(8, -10, 10) no_repeat_numbers = create_unique_number_list(5, 0, 10) letters = create_random_letter_list(8) numbers.sort() no_repeat_numbers.sort() print(numbers) print(no_repeat_numbers) print(letters) main()
Run the program again. You should see extra output.
11.7.1. Why Did We Do This?¶
We need to demonstrate one last detail about how to correctly use a
Return to the
main.py code. In this case, we want the
to run. However, ANY
.py file can be imported as a module into any other
If we want to use the
create_report functions in
a different program, then we can easily import
main.py into that other
file. However, as it is written, lines 20 - 27 will ALWAYS run whenever we
import the module.
We need to adjust our code so that
main() executes when it is part of the
central program but NOT when it is part of an imported module.
11.7.2. Wrapping Up¶
Before Python runs a program, it defines a few special variables. One of
these is called
__name__, and it is automatically assigned the string value
"__main__" when the code runs by itself. For an imported module, on the
__name__ matches the module name.
This means we can use
__name__ to check if the code is being run by itself,
or if it has been imported by another program!
random_lists.py, replace the function call:
if __name__ == "__main__": main()
The program should correctly run again!
The first line uses an
if statement to check the value of the
variable. If the value matches
"__main__", then the
gets called. If the condition is
False, then the code was imported as a
module, and we do NOT call
The ability to choose when to execute the
main() function is extremely
useful! This is especially true for code that might be imported into a
Whew! That was a lot of words and code. If your eyes glazed over, be sure to pay attention to this:
Avoid loose statements in your code.
main()function, and set it up this way:
def main(): # Control code here... if __name__ == "__main__": main()