Using a main check __main__ to call a main function in Python
Posted by jpluimers on 2020/01/01
[WayBack] __main__ — Top-level script environment — Python 3 documentation recommends code like this:
if __name__ == "__main__":
# execute only if run as a script
main()
This has many cool possibilities, including these that I like most as a beginning Python developer:
- having your
def main():function in a separate source file - allowing to
returnprematurely from yourmainfunction (you cannot do this from the main block) - allowing a file to distinguish if it is being loaded as a module, or as a main program
Related:
–jeroen






Leave a comment