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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:32:35+00:00 2026-05-13T09:32:35+00:00

my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.)

  • 0

my python code is interlaced with lots of function calls used for (debugging|profiling|tracing etc.)
for example:

import logging

logging.root.setLevel(logging.DEBUG)
logging.debug('hello')
j = 0
for i in range(10):
    j += i
    logging.debug('i %d j %d' % (i,j))
print(j)
logging.debug('bye')

i want to #define these resource consuming functions out of the code. something like the c equivalent

#define logging.debug(val)

yes, i know the logging module logging level mechanism can be used to mask out loggings below set log level. but, im asking for a general way to have the python interpreter skip functions (that take time to run even if they dont do much)

one idea is to redefine the functions i want to comment out into empty functions:

def lazy(*args): pass
logging.debug = lazy

the above idea still calls a function, and may create a myriad of other problems

  • 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-13T09:32:35+00:00Added an answer on May 13, 2026 at 9:32 am

    Python does not have a preprocessor, although you could run your python source through an external preprocessor to get the same effect – e.g. sed "/logging.debug/d" will strip out all the debug logging commands. This is not very elegant though – you will end up needing some sort of build system to run all your modules through the preprocessor and perhaps create a new directory tree of the processed .py files before running the main script.

    Alternatively if you put all your debug statements in an if __debug__: block they will get optimised out when python is run with the -O (optimise) flag.

    As an aside, I checked the code with the dis module to ensure that it did get optimised away. I discovered that both

    if __debug__: doStuff()
    

    and

    if 0: doStuff()
    

    are optimised, but

    if False: doStuff()
    

    is not. This is because False is a regular Python object, and you can in fact do this:

    >>> False = True
    >>> if False: print "Illogical, captain"
    Illogical, captain
    

    Which seems to me a flaw in the language – hopefully it is fixed in Python 3.

    Edit:

    This is fixed in Python 3: Assigning to True or False now gives a SyntaxError.
    Since True and False are constants in Python 3, it means that if False: doStuff() is now optimised:

    >>> def f():
    ...     if False: print( "illogical")
    ... 
    >>> dis.dis(f)
      2           0 LOAD_CONST               0 (None) 
                  3 RETURN_VALUE         
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to file.flush to write it out. You can… May 14, 2026 at 6:49 pm
  • Editorial Team
    Editorial Team added an answer The ajax request that you make with jQuery will send… May 14, 2026 at 6:49 pm
  • Editorial Team
    Editorial Team added an answer Assuming you are parsing a string and it has at… May 14, 2026 at 6:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.