Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Boilerplate" code in Python?

Google has a Python tutorial, and they describe boilerplate code as "unfortunate" and provide this example:

#!/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() 

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" (example).

Anyways, in Python, the part considered "boilerplate" code of the example above was:

if __name__ == '__main__':   main() 

Now, my questions are as follows:

1) Does boilerplate code in Python (like the example provided) take on the same definition as the definition I provided? If so, why?

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?

3) Why do we use that code and what service does it provide?

4) Does this occur throughout Python? Are there other examples of "boilerplate code"?

Oh, and just an off topic question: do you need to import sys to use command line arguments in Python? How does it handle such arguments if its not there?

like image 238
Bhaxy Avatar asked Oct 26 '11 02:10

Bhaxy


People also ask

What is boilerplate code python?

In computer programming, boilerplate code, or simply boilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered verbose, the programmer must write a lot of boilerplate code to accomplish only minor functionality.

What is boilerplate code?

In computer programming, boilerplate code or boilerplate refers to sections of code that have to be included in many places with little or no alteration. It is often used when referring to languages that are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs.

What is a boilerplate example?

Another example of a boilerplate is the fine print that appears on many contracts. This section is usually static, as is the case with many cell phone contracts.


1 Answers

  1. It is repetitive in the sense that it's repeated for each script that you might execute from the command line.
  2. If you put your main code in a function like this, you can import the module without executing it. This is sometimes useful. It also keeps things organized a bit more.
  3. Same as #2 as far as I can tell
  4. Python is generally pretty good at avoiding boilerplate. It's flexible enough that in most situations you can write code to produce the boilerplate rather then writing boilerplate code.

Off topic question:

If you don't write code to check the arguments, they are ignored.

like image 119
Winston Ewert Avatar answered Sep 25 '22 20:09

Winston Ewert