Sunday 25 March 2018

python - Why use def main()?











I've seen some code samples and tutorials that use




def main():
# my code here

if __name__ == "__main__":
main()


But why? Is there any reason not do define your functions at the top of the file, then just write code under it? ie




def my_function()
# my code here

def my_function_two()
# my code here

# some code
# call function
# print(something)



I just wonder if there is any rhyme to the main?


Answer



Without the main sentinel, the code would be executed even if the script were imported as a module.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...