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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:59:05+00:00 2026-06-04T03:59:05+00:00

According to python documentation assert statements aren’t included in code if it is compiled

  • 0

According to python documentation assert statements aren’t included in code if it is compiled with -O key. I wondering if it is possible to emulate this behavior with any arbitrary piece of code?

For example, if I have a logger which is called heavily during execution I can benefit from eliminating if DEBUG: ... statements with all the code associated to them.

  • 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-06-04T03:59:06+00:00Added an answer on June 4, 2026 at 3:59 am

    Since Python is an interpreted language, it can’t jump around in its own code. But you don’t need a “special tool” to strip off parts of your code — use Python for it!

    Here is a minimal example. You’ll probably want to put the strip_debug() function in your __init__.py and let it process a list of modules. Also, you’d probably want to add some additional check that the user really wants to modify the code, not just run it. Probably, using a command line option like --purge would be nice. You can then either make a copy of your library and once run

    python __init__.py --purge
    

    before you publish the library or leave it up to your users to do so.

    #!/usr/bin/env python3.2
    
    # BEGIN DEBUG
    def _strip_debug():
        """
        Generates an optimized version of its own code stripping off all debugging
        code.
    
        """
        import os
        import re
        import shutil
        import sys
        import tempfile
        begin_debug = re.compile("^\s*#+\s*BEGIN\s+DEBUG\s*$")
        end_debug = re.compile("^\s*#+\s*END\s+DEBUG\s*$")
        tmp = None
        debug = False
        try:
            tmp = tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False)
            with open(sys.argv[0]) as my_code:
                for line in my_code:
                    if begin_debug.match(line):
                        debug = True
                        continue
                    elif end_debug.match(line):
                        debug = False
                        continue
                    else:
                        if not debug:
                            tmp.write(line)
            tmp.close()
            shutil.copy(tmp.name, sys.argv[0])
        finally:
            os.unlink(tmp.name)
    # END DEBUG    
    
    def foo(bar, baz):
        """
        Do something weired with bar and baz.
    
        """
        # BEGIN DEBUG
        if DEBUG:
            print("bar = {}".format(bar))
            print("baz = {}".format(baz))
        # END DEBUG
        return bar + baz
    
    
    # BEGIN DEBUG
    if __name__ == "__main__":
        _strip_debug()
    # END DEBUG
    

    After executed, this file will only contain the functional code of the foo() function. I have used the special comments

    # BEGIN DEBUG
    

    and

    # END DEBUG
    

    in this example which allows to strip off arbitrary code but if it’s just for removing

    if DEBUG:
        # stuff
    

    sections, it would also be pretty easy to detect those without any additional comments.

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

Sidebar

Related Questions

According to Jython's documentation : Jython is an implementation of the Python language for
According to my interpretation of Python 2.7.2 documentation for Built-In Types 5.7 Set Types
According to the Python documentation, only a few hash algorithms are guaranteed to be
According to the official Python documentation for the weakref module the primary use for
I need to execute some dynamically generated code in Python and ensure that this
According to the documentation on python's getopt (I think) the options fields should behave
What exactly do *args and **kwargs mean? According to the Python documentation, from what
According to the python documentation exception OverflowError Raised when the result of an arithmetic
According to the GIO Documentation, it is possible to get the Icon of a
According to the documentation from python.org, python 3.2 install on mac os requires an

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.