title: Why def main(argv=[__name__]) and if __name__ == "__main__": sys.exit(main(sys.argv))?
body: <p>I'm working with/rewriting a code that first defines a function as follows:</p>
  
  <pre><code>def main(argv=[__name__]):
      ...
      *rest of code*
      ...
  </code></pre>
  
  <p>and ends with:</p>
  
  <pre><code>if __name__ == "__main__":
      sys.exit(main(sys.argv))
  </code></pre>
  
  <p>I'm under the impression that what this is doing is checking to make sure that the script is being executed from the command line, and then running the function <code>main</code> with the arguments provided as it exits python. But then, why is it necessary to preset the variable <code>argv</code> to <code>[__name__]</code> in the function definition? This is not my code, so I don't know the original intention behind this. I am, however, new to using <code>if __name__ == "__main__":</code> lines to spot check command line execution, so maybe there is some glaringly obvious reason for this syntax that I'm missing. Any help, or further detail on <code>main</code> function definition and argument/command-line-vs-module testing would be appreciated.</p>