Many programming languages have a special functions that is automatically executed when an operation system starts to run.
This function is often called the entry point because it is where execution enters the program.
The Python interpreter is not have special functions that serves as the entry point
Nevertheless (pt:mesmo assim) have convetions to define start execution.
def main():
print("Hello World!")
if __name__ == "__main__":
# execute only if run as a script
main()
- Put most code into a function or class.
- Use
__name__
to control execution of your code. - Create a function called main() to contain the code you want to run.
- Call other functions from main().
Sometimes the code you write will have side effects that you want the user to control, such as:
- Running a computation that takes a long time
- Writing to a file on the disk
- Printing information that would clutter the user’s terminal
why?
This is because when the Python interpreter encounters the def or class keywords, it only stores those definitions for later use and doesn’t actually execute them until you tell it to.
Example: best_pratices.py
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
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
if __name__ == "__main__":
data = "My data read from the Web"
print(data)
modified_data = process_data(data)
print(modified_data)
Although Python does not assign any significance to a function named main(), the best practice is to name the entry point function main() anyways.
why?
That way, any other programmers who read your script immediately know that this function is the starting point of the code that accomplishes the primary task of the script.
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 main():
data = "My data read from the Web"
print(data)
modified_data = process_data(data)
print(modified_data)
if __name__ == "__main__":
main()
The function main()
must execute other functions.
To reuse functionality
Examples:
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()
Now, you can run the whole processing pipeline from the command line, as shown below:
$ python3 best_practices.py
This is my file to demonstrate best practices.
Reading data from the Web
Beginning data processing...
Data processing finished.
Writing processed data to a database
Data from the web that has been modified
- Some Gist
- Python Docs: __main__
- Stack Exchange: DB connections
- Real Python: main function
- Real Python: exceptions
- Real Python: handlers in logging
- Stack Overflow: method "new"
- Stack Overflow: DB how to add foreign key
- Stack Overflow: create user if inexistent in MySQL
- Stack Overflow: manually raising throwing exception