Understanding the if __name__ == '__main__' Syntax
Syntax:
def run():
print("the process is starting")
def shut_down():
print("process is shutting down")
def email_admin():
print("Violation noted. Emailing admin...")
# Starting point of the application
if __name__ == '__main__':
email_admin() # First task running
shut_down() # Second task running
It can also be imported:
from process import *
run()
shut_down()
Understanding the syntax
the variable has underscores in order to not be easy to overwrite by accident
When you run the file directly the
__name__
variable is assigned the__main__
valueBasically if it is defined then it runs the instructions
If the
if __name__ == '__main__'
is not specified then it will run the file as isFor the above case it will run
run
,shut_down
,email_admin
Last updated