18

I occasionally notice something like the following in Python scripts:

if __name__ == "__main__":
    # do stuff like call main()

What's the point of this?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Richard Simões
  • 12,401
  • 6
  • 41
  • 50
  • After you read about this in the Python documentation, what *specific* question can you ask? See http://docs.python.org/library/python.html for example. After you read this section, what *specific* questions do you have. – S.Lott Dec 23 '09 at 19:06
  • 5
    S.Lott: His question seems specific enough. He's asking what that condition is generally for... – Fragsworth Dec 23 '09 at 19:10
  • I was wondering about this question myself last night. – Poindexter Dec 23 '09 at 19:13
  • @Fragsworth: I can't see how this is "specific". "What's the point of..." questions are open-ended. They don't solve a programming problem. Also, the answer to "what's the point" is in the documentation. It would be nice to have a more focused question that isn't -- trivially -- part of the documentation. – S.Lott Dec 23 '09 at 19:19
  • The answer to this question can be found quickly using Google (probably more quickly than taking the time to type the question). I don't see why this question got upvoted 5 times (so far). Questions like this create a lot of noise and just duplicate existing information that is easily found. – new name Dec 23 '09 at 19:36
  • Alright, alright, mea culpa (voted to close). – Richard Simões Dec 23 '09 at 19:58

4 Answers4

27

Having all substantial Python code live inside a function (i.e., not at module top level) is a crucial performance optimization as well as an important factor in good organization of code (the Python compiler can optimize access to local variables in a function much better than it can optimize "local" variables which are actually a module's globals, since the semantics of the latter are more demanding).

Making the call to the function conditional on the current module being run as the "main script" (rather than imported from another module) makes for potential reusability of nuggets of functionality contained in the module (since other modules may import it and just call the appropriate functions or classes), and even more importantly it supports solid unit testing (where all sort of mock-ups and fakes for external subsystems may generally need to be set up before the module's functionality is exercised and tested).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • I shall never put all my code in the module top level again. Thank you. – levesque May 10 '11 at 18:49
  • This was a better explanation for me than the one with thousands of votes. I couldn't really see the point even after all the examples. Thanks! – carkod Mar 26 '19 at 11:12
8

This allows a python script to be imported or run standalone is a sane way.

If you run a python file directly, the __name__ variable will contain __main__. If you import the script that will not be the case. Normally, if you import the script you want to call functions or reference classes from the file.

If you did not have this check, any code that was not in a class or function would run when you import.

Jesse Vogt
  • 16,229
  • 16
  • 59
  • 72
7

The sole purpose of this, assuming it is in main.py, is so other files can import main to include classes and functions that are in your "main" program, but without running the source code.

Without this condition, code that is in the global scope will be executed when it is imported by other scripts.

Fragsworth
  • 33,919
  • 27
  • 84
  • 97
3

It's a great place to put module tests. This will only run when a module is run directly from the shell but it will not run if imported.

flexterra
  • 103
  • 1
  • 1
  • 6