Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6736485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:08:06+00:00 2026-05-26T11:08:06+00:00

Google has a Python tutorial, and they describe boilerplate code as unfortunate and provide

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:08:06+00:00Added an answer on May 26, 2026 at 11:08 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why isn't this simple Python code working? import urllib file = urllib.urlopen('http://www.google.com') print file.read()
My Google-fu has failed me. In Python, are the following two tests for equality
On Google App Engine in Python, I have a Visit entity that has a
I'm a beginner in Python, teaching myself off of Google Code University. I had
Does Google has a service similar to Yahoo! Placemaker: Developers specify structured and unstructured
It is known that google has best searching & indexing algorithm. The also have
Is there any free hosting for Javascript? Recently google has been hosting jQuery,etc... and
Google guice has a built-in logger binding . But what if I want to
My google-fu has failed me - can MonoDevelop be used on Windows? Preferably without
Now that Google Earth has been released for Android 2.1, does anyone know what,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.