Python
from Defining Main Functions in Python
Summary of Python Main Function Best Practices
Here are four key best practices about main() in Python that you just saw:
Put code that takes a long time to run or has other effects on the computer in a function or class, so you can control exactly when that code is executed.
Use the different values of __name__ to determine the context and change the behavior of your code with a conditional statement.
You should name your entry point function main() in order to communicate the intention of the function, even though Python does not assign any special significance to a function named main().
If you want to reuse functionality from your code, define the logic in functions outside main() and call those functions within main().
from time import sleep
print("This is my file to demonstrate best practices.")
def process_data(data):
print("Beginning data processing...")
modified_data = data + " that has been modified"
sleep(3)
print("Data processing finished.")
return modified_data
def read_data_from_web():
print("Reading data from the Web")
data = "Data from the web"
return data
def write_data_to_database(data):
print("Writing data to a database")
print(data)
def main():
data = read_data_from_web()
modified_data = process_data(data)
write_data_to_database(modified_data)
if __name__ == "__main__":
main()
Python
main — Top-level script environment
'main' is the name of the scope in which top-level code executes. A module’s name is set equal to 'main' when read from standard input, a script, or from an interactive prompt.
A module can discover whether or not it is running in the main scope by checking its own name, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m but not when it is imported:
if __name__ == "__main__":
# execute only if run as a script
main()
For a package, the same effect can be achieved by including a main.py module, the contents of which will be executed when the module is run with -m.