What is it? #
Conditional statement that runs a particular chunk of code if the file is called directly.
Structure #
def main():
...
# oftentimes, calls to functions defined higher up in the script
if __name__ == '__main__':
main()
__name__
#
print __name__
# __main__
Whenever Python runs, it sets a bunch of special variables, including __name__
. A Python script that is called directly causes __name__
to be set to __main__
.