2

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")
Mugen
  • 1,417
  • 5
  • 22
  • 40
  • 5
    You can import and test or reuse main elsewhere, you can't do that with inline code. – jonrsharpe Feb 18 '20 at 08:22
  • 1
    @Barmar - The question has nothing to do with the if statement, its about whether its contents need to be encapsulated. – Sayse Feb 18 '20 at 08:29
  • 1
    @Sayse Understanding why we use the if statement in general should explain why the contents need to be encapsulated. – Barmar Feb 18 '20 at 08:30
  • Reopened the question as it was not a dup of https://stackoverflow.com/questions/419163 – bruno desthuilliers Feb 18 '20 at 09:16
  • 1
    @tripleee would you mind reopening this question ? It is **NOT** asking what the `if __name__ == '__main__'` guard is doing, it's asking "why put the main code in a main function". – bruno desthuilliers Feb 18 '20 at 11:52
  • The two questions are closely related and the duplicate covers both quite well IMHO. If you disagree, I'd think posting a new answer to the duplicate would be more appropriate. – tripleee Feb 18 '20 at 11:54
  • 1
    @tripleee I totally disagree indeed. The OP clearly states he knows what the guard is doing and why we're using it - "This prevents executing statements when you want to import this module into another program." -, what he asks is why putting the main code in a function instead of having it directly under the guard. I definitly don't see how this is a dup. – bruno desthuilliers Feb 18 '20 at 12:02
  • @tripleee "If you disagree, I'd think posting a new answer to the duplicate would be more appropriate" => certainly not, because those are totally unrelated questions - cf jonrsharpe ans Sayse comments too - _they_ did read and get the question instead of stopping at the mention of the "main" guard and concluding the question was about the use of the guard. – bruno desthuilliers Feb 18 '20 at 12:08
  • 1
    Fine; let's hope these comments also prompt visitors to review the answers there. – tripleee Feb 18 '20 at 12:49

1 Answers1

4

However, the curious thing over here is that main() function. We also need to put our code into an actual main method as follows:

Well, you dont need to do so. Actually if all you want is a plain script (something that's not going to be used as a module), you don't even "need" any function at all, you could as well put all your code directly at the top-level. BUT this will not make for clean, maintainable, testable code.

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

Quite simply the same benefits as you get from using functions (and modules and classes) to organize your code: clean, readable, testable, maintainable code. Just ask yourself how you are going to test your main function if you don't have a main function ?

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • I don't feel that this answers the question. This answers essentially states that we're adding a main() function because we're just putting everything into functions. However, if we have the code under the if-clause. Also, what if all that main() code goes inside the if-clause? That's still organized IMHO because if you look at the script you will look directly at the if-clause and know that that is what will get run. – Mugen Feb 18 '20 at 11:19
  • 1
    Well, you ask mainly two things: 1/ why we "need" to put the main code into a function, and 2/ what are the benefits. The answers are 1/ you actually don't "need" to do so, and 2/ the main benefit is that you can call this "main" code as function, which allow you to a/ unittest your main function (how do you unittest this code else ?) and b/ possibly call this main function (from another module / script / whatever) too. And this answers the "what are the harms of not doing so": you cannot unit test this part of the code nor call this code from another module. – bruno desthuilliers Feb 18 '20 at 11:40
  • I'm sorry, I didn't get time to read your comments for 4 hours, until now. I read your comments and they answer my question just fine. Thanks a lot for replying! :) – Mugen Feb 18 '20 at 15:51
  • I just went through the comments thread on the question. Thanks for reopening my question and insisting that it's different than the other one. I really appreciate your help! – Mugen Feb 18 '20 at 15:55