Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Boilerplate" code in Python?
    primarykey
    data
    text
    <p>Google has a Python tutorial, and they describe boilerplate code as "unfortunate" and provide this example:</p> <pre><code>#!/usr/bin/python # import modules used here -- sys is a very standard one import sys # Gather our code in a main() function def main(): print 'Hello there', sys.argv[1] # Command line args are in sys.argv[1], sys.argv[2] .. # sys.argv[0] is the script name itself and can be ignored # Standard boilerplate to call the main() function to begin # the program. if __name__ == '__main__': main() </code></pre> <p>Now, I've heard boilerplate code being described as "seemingly repetitive code that shows up again and again in order to get some result that seems like it ought to be much simpler" (<a href="https://stackoverflow.com/questions/3992199/what-is-boilerplate-code">example</a>).</p> <p>Anyways, in Python, the part considered "boilerplate" code of the example above was:</p> <pre><code>if __name__ == '__main__': main() </code></pre> <p>Now, my questions are as follows:</p> <p>1) Does boilerplate code in Python (like the example provided) take on the same definition as the definition I provided? If so, why?</p> <p>2) Is this code even necessary? It seems to me like the code runs whether or not there's a main method. What makes using this code better? Is it even better?</p> <p>3) Why do we use that code and what service does it provide?</p> <p>4) Does this occur throughout Python? Are there other examples of "boilerplate code"?</p> <p>Oh, and just an off topic question: do you need to <code>import sys</code> to use command line arguments in Python? How does it handle such arguments if its not there?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload