2

Preface:

By convention, Python scripts that are run directly and not imported have the line if __name__ == '__main__':. This is to prevent code that should not be executed from running. It can also be used the other way around to only allow code to be run if it is not executed directly, if __name__ != '__main__':

Question:

Are there any benefits/downsides to calling an arbitrarily named "main" function inside the conventional if statement mentioned above? For example:

def main() -> None:
    print('Done')
    return

if __name__ == '__main__':
    main()

Research / Knowledge So Far:

Benefits:

  • Easier and cleaner exit of execution
    • Can use return
    • No longer need to import sys and use sys.exit(0)
  • Control scope of variables more easily
    • No "shadowed" variable names that would otherwise be found "globally"
    • Variables in the "main" function are only seen in the "main" function (due to scope)
  • Easier to call the "main" function from another script

Downsides:

  • It is not necessary
  • It can look more cluttered (more coding involved)

Further Question:

What other benefits and/or downsides are there? Does it make the interpreter do more work if the code is in a function as opposed to not in a function?

VoidTwo
  • 569
  • 1
  • 7
  • 26
  • 1
    `Easier to call the "main" function from another script` - If you practice this often enough developing packages comes much more natural. But modularity is definitely the biggest thing here, being able to call code from other programs, tie in multiple scripts, etc. – Ironkey Nov 19 '20 at 04:24
  • Does this answer your question? [What does if \_\_name\_\_ == "\_\_main\_\_": do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – kennysliding Nov 19 '20 at 04:26
  • @BurningAlcohol I know what `if __name__ == '__main__':` is. My question was more about the pros and cons of using it. – VoidTwo Nov 19 '20 at 04:28
  • 2
    There is no downside to using a `main` function and calling it in the `if __name__ == "__main__"`. In fact, as a personal anecdote: I submitted some Python code as part of an interview and one of the critiques I got upon code review was that I did not encapsulate my entrypoint code into a `main` function. – Adam Smith Nov 19 '20 at 04:30
  • I agree to @AdamSmith's comment, the only downside I can think of is that it increases the file size, which you also mentioned. – kennysliding Nov 19 '20 at 04:33

1 Answers1

2

It is all about code/module management, without the main sentinel, the code would be executed even if the script were imported as a module.

kennysliding
  • 2,783
  • 1
  • 10
  • 31
  • I mentioned this in my preface, but I appreciate the further detail – VoidTwo Nov 19 '20 at 04:27
  • You've actually done a very good research on the topics. Since Python does not default come with a `main` function-kind of concept, unlike in other languages such as C++ where you have `int main()`, this is the most common used practice. – kennysliding Nov 19 '20 at 04:28