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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:48:20+00:00 2026-06-01T02:48:20+00:00

I place many checks of the program state, whose failure would indicate a bug

  • 0

I place many checks of the program state, whose failure would indicate a bug in the code. In such cases, I’d love to use assert condition simply because it reads nicer than if not condition: raise MyException.

Using assert instead of raise has two problems.

  1. assert does not allow to specify the exception to be raised, so catching it later becomes hard (catching AssertionError may catch too much).

  2. assert is disabled when -O flag is passed to the interpreter.

In my environment any bug in the code requires that I discard any results until the bug is identified and fixed. Therefore, there’s no point catching exceptions raised by the above checks. Thus, problem #1 is irrelevant in my situation.

Problem #2 is serious. I want my checks to remain in production code, since correctness is far more important than performance in my environment. Few people use -O flag today; but one day I or someone else may prefer to use it (e.g., to suppress code behind if __debug__, or because -O might actually optimize code in the future). Since all my assert statements must remain active in the production code, I’ll need to replace all assert statements with something else. Is there any way to force assert to stay despite the -O flag?

By the way, I’m planning to customize the behavior of assert by printing variable values from the line that caused the assert to fail. I think I can do it by replacing sys.excepthook with my own function that catches AssertionError, reads the traceback, finds the relevant source code, prints the variables from the relevant line, and then reraises the exception. If anyone sees a problem with that, please let me know.

  • 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-01T02:48:21+00:00Added an answer on June 1, 2026 at 2:48 am

    Just don’t use assertions if they aren’t what you need. Instead be explicit that you’ll throw this exception if some condition is violated. The assert statement always carries a “I may or may not be run at all” side. It’s less hacky, less astonishing, less likely to break in the future, and does not require you to prevent users from adding -O.

    If you just want to save typing, you can do so. Create a function like this (a more specific name is highly recommended) and use it instead of assert:

    def require(cond, msg):
        if not cond:
            raise MyException(msg)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In how many ways and places can we place comments in CSS? Can the
In many places in our code we have collections of objects, from which we
In many places in our application we have code like this: using(RAPI rapi =
In my code I have a Category model which has many subcategories, and the
I can see many place used console.log() . Can anyone tell me why it
I've put all of my user-authentication code in one place, namely lib/auth.rb. It looks
There are cases when you have many UI updates due a massive amount of
I have a Silverlight datagrid that allows the use to enter in how many
For example, what Wordpad did when I press Ctrl+C? It places many different format
In many places, (1,2,3) (a tuple) and [1,2,3] (a list) can be used interchangeably.

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.