Sunday, March 25, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...