Jul-21-2020, 04:05 AM
The purpose of "if __name__ == "__main__:" is to prevent executing statements when the file is imported. If I wanted to use your user_input class I could import your module. When I do this I would not want to run your main() function, and the if __name__ == "__main__" prevents that from happening because your file is no longer the "__main__" file.
As for "main()" always being the first function to get executed, there is nothing special about using "main()" as a function name other than convention. You could call your main function "doit()" or something else, or you could not even have a main function. For example, your module would work exactly the same written this way:
As for "main()" always being the first function to get executed, there is nothing special about using "main()" as a function name other than convention. You could call your main function "doit()" or something else, or you could not even have a main function. For example, your module would work exactly the same written this way:
class user_input(): print("This is class") print(user_input()) if __name__ == "__main__": print("This is main")