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 6669813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:11:41+00:00 2026-05-26T03:11:41+00:00

Python has a few ways of printing trace output. print , import logging ,

  • 0

Python has a few ways of printing “trace” output. print, import logging, stdout.write can be used to print debugging info, but they all have one drawback: even if the logger’s threshold is too high or the stream is closed, Python will still evaluate the arguments to the print statement. (Strict Evaluation) This could cost a string format or more.

The obvious fix is to put the string-creating code into a lambda, and use our own logging function to call the lambda conditionally (this one checks the __debug__ builtin variable, which is set to False whenever python is started with -O for optimizations) :

def debug(f):
  if __debug__:
    print f()
    #stdout.write(f())
    #logging.debug(f())

for currentItem in allItems:
  debug(lambda:"Working on {0}".format(currentItem))

The advantage is not calling str(currentItem) and string.format in release builds, and the disadvantage is having to type in lambda: on every logging statement.

Python’s assert statement is treated specially by the Python compiler. If python is run with -O, then any assert statements are discarded without any evaluation. You can exploit this to make another conditionally-evaluated logging statement:

assert(logging.debug("Working on {0}".format(currentItem)) or True)

This line will not be evaluated when Python is started with -O.

The short-circuit operators ‘and’ and ‘or’ can even be used:

__debug__ and logging.debug("Working on {0}".format(currentItem));

But now we’re up to 28 characters plus the code for the output string.

The question I’m getting to: Are there any standard python statements or functions that have the same conditional-evaluation properties as the assert statement? Or, does anyone have any alternatives to the methods presented here?

  • 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-26T03:11:41+00:00Added an answer on May 26, 2026 at 3:11 am

    I wonder how much a call to logging.debug impacts the performance when there are no handlers.

    However the if __debug__: statement is evaluated only once, even in the body of a function

    $ python -O
    Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import dis
    >>> import logging
    >>> def debug(*a, **kw):
    ...  if __debug__:
    ...   logging.debug(*a, **kw)
    ... 
    >>> dis.dis(debug)
      2           0 LOAD_CONST               0 (None)
                  3 RETURN_VALUE        
    >>> 
    

    and the logger can format the message for you using the string formatting operator. Here a slightly modified example taken from the logging.debug documentation

    FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
    logging.basicConfig(format=FORMAT)
    d = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
    debug('Protocol problem: %s', 'connection reset', extra=d)
    

    In this case the message string is never evaluated if the optimizations are turned off.

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

Sidebar

Related Questions

I am using Python for automating a complex procedure that has few options. I
Python has a flag -O that you can execute the interpreter with. The option
I know that python has a len() function that is used to determine the
Python language has a well known feature named interactive mode where the interpreter can
I want to create an object in python that has a few attributes and
I have been working in Python for a few months now, and it has
One of my favorite features about python is that you can write configuration files
I'm building a service, which has a few cronjobs running, written in Python. However,
For a few days now, my App Engine logging console has been behind for
I have inherited a few Python scripts from someone who has left my employer.

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.