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

  • SEARCH
  • Home
  • 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 4609708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:01:39+00:00 2026-05-22T01:01:39+00:00

I know the –verbose or -v from several tools and I’d like to implement

  • 0

I know the --verbose or -v from several tools and I’d like to implement this into some of my own scripts and tools.

I thought of placing:

if verbose:
    print ...

through my source code, so that if a user passes the -v option, the variable verbose will be set to True and the text will be printed.

Is this the right approach or is there a more common way?

Addition: I am not asking for a way to implement the parsing of arguments. That I know how it is done. I am only interested specially in the verbose option.

  • 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-22T01:01:40+00:00Added an answer on May 22, 2026 at 1:01 am

    My suggestion is to use a function. But rather than putting the if in the function, which you might be tempted to do, do it like this:

    if verbose:
        def verboseprint(*args):
            # Print each argument separately so caller doesn't need to
            # stuff everything to be printed into a single string
            for arg in args:
               print arg,
            print
    else:   
        verboseprint = lambda *a: None      # do-nothing function
    

    (Yes, you can define a function in an if statement, and it’ll only get defined if the condition is true!)

    If you’re using Python 3, where print is already a function (or if you’re willing to use print as a function in 2.x using from __future__ import print_function) it’s even simpler:

    verboseprint = print if verbose else lambda *a, **k: None
    

    This way, the function is defined as a do-nothing if verbose mode is off (using a lambda), instead of constantly testing the verbose flag.

    If the user could change the verbosity mode during the run of your program, this would be the wrong approach (you’d need the if in the function), but since you’re setting it with a command-line flag, you only need to make the decision once.

    You then use e.g. verboseprint("look at all my verbosity!", object(), 3) whenever you want to print a "verbose" message.

    If you are willing and able to use the Python -O flag to turn verbosity on and off when launching the "production" version of the script (or set the PYTHONOPTIMIZE environment variable) then the better way is to test the __debug__ flag everywhere you want to print a verbose output:

    if __debug__: print("Verbosity enabled")
    

    When run without optimization, these statements are executed (and the if is stripped out, leaving only the body of the statement). When run with optimization, Python actually strips those statements out entirely. They have no performance impact whatsoever! See my blog post on __debug__ and -O for a more in-depth discussion.

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

Sidebar

Related Questions

Know of any good libraries for this? I did some searches and didn't come
I know some people use ANT to accomplish this, but I don't want to
I know this question would have been asked several time but still I am
I know I could use pagination gems to accomplish a task like this, but
Know this might be rather basic, but I been trying to figure out how
I know that I can do something like $int = (int)99; //(int) has a
I know this might be a no-brainer, but please read on. I also know
I know, I would have thought the answer was obviously no as well, but
I know about Papervision 3D. However, alot of the realism there comes from textures.
I know that this is a very commonly asked question, but all of the

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.