It is said to be a good practice to write the following block of code in your python modules:
if __name__ == '__main__':
# execute only if run as a script
main()
This prevents executing statements when you want to import this module into another program.
However, the curious thing over here is that main() function. We also need to put our code into an actual main method as follows:
def main()
# All code goes here
print("Inside main")
I feel that this is a roundabout way. Are there any benefits to creating a separate main() function and then calling it inside the if-clause?
Instead what is the harm in writing the code directly inside the if-clause?
if __name__ == '__main__':
# All code goes here
print("Inside main")