Many Python scripts include a function called main() that contains the main set of actions the script performs. In this post, you learn the purpose of the main() function and when and how to create one.
You also learn about the two ways to run code using the Python interpreter. How you run a script affects how Python sets the built‐in __name__ parameter, which you can use to control whether the main() function runs.
As its name suggests, the main() function typically forms the core part of a Python script. You would normally use the main() function in conjunction with an if statement that checks the value of the __name__ parameter. Doing so enables your script to determine whether it was launched from the command line or whether it was imported into the interpreter or into another script or module.
Here is pseudocode showing a main() function and its if statement, with italics indicating a placeholder:
def main():
statements
if __name__ == "__main__":
main()
Here is how this works:
• def. This keyword starts the definition of the function.
• main():. This is the name of the function, followed by a colon to end the line. This line is called the function header.
• statements. This is where you place statements that specify the actions the main()function is to take. The statements are indented by four spaces to indicate that they belong to the function’s block of code.
• if. This keyword begins the condition, which compares the value of the __name__ parameter to the string "__main__". Two equal signs, ==, denote equality. The double quotes, ", mark the beginning and end of a literal string of text. The colon ends the line.
• main(). This statement tells Python to execute the main() function if the condition evaluates to True.
This statement is indented by four spaces to show it belongs to the if statement’s block of code.
When to Create a main() Function?
Create a main() function in any script that you want to have execute in a different way when it is run from the command line than when it has been imported into the interactive interpreter or into another script or module.
Understanding the Two Ways to Run Python Code
You can run a Python script either by launching it from the command line or by importing it into the interactive interpreter or another Python file.
Launch a Script via the Command Line
The first way to launch a script is by using the command line. You start by opening a terminal window, such as a Command Prompt window on Windows or a window in the Terminal app on macOS or Linux. You then navigate to the appropriate folder, type the Python app’s name and the script’s name, and press enter.
For example, to run the script called my_script.py from the current folder, you might use this command on Windows:
python myscript.py
Or you could use this command on macOS or Linux:
python3 my_script.py
When you launch a script from the command line, Python sets the script’s __name__ parameter to __main__.
Import a Script into the Interactive Interpreter or into Another Script or Module
To import a script, you use the import keyword followed by the script’s name without its extension. For example, if the script’s name is acme_calcs.py, you can import it using the following statement:
import acme_calcs
When you import a script into the interactive interpreter, into another script, or into another module, Python sets the script’s __name__ parameter to the script’s name without the extension. Continuing the previous example of importing, Python sets the __name__ parameter to acme_calcs.
0 comments:
Post a Comment