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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:22:34+00:00 2026-06-14T09:22:34+00:00

In Python, assert is a statement, and not a function. Was this a deliberate

  • 0

In Python, assert is a statement, and not a function. Was this a deliberate decision? Are there any advantages to having assert be a statement (and reserved word) instead of a function?

According to the docs, assert expression1, expression2 is expanded to

if __debug__:
    if not expression1: raise AssertionError(expression2)

The docs also say that “The current code generator emits no code for an assert statement when optimization is requested at compile time.” Without knowing the details, it seems like a special case was required to make this possible. But then, a special case could also be used to optimize away calls to an assert() function.

If assert were a function, you could write:

assert(some_long_condition,
       "explanation")

But because assert is a statement, the tuple always evaluates to True, and
you get

SyntaxWarning: assertion is always true, perhaps remove parentheses?

The correct way to write it is

assert some_long_condition, \
       "explanation"

which is arguably less pretty.

  • 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-14T09:22:36+00:00Added an answer on June 14, 2026 at 9:22 am

    Are there any advantages to having assert be a statement (and reserved word) instead of a function?

    1. Cannot be reassigned to a user function, meaning it can be effectively disabled at compile time as @mgilson pointed out.
    2. The evaluation of the second, optional parameter is deferred until if/when the assertion fails. Awkward to do that with functions and function arguments (would need to pass a lambda.) Not deferring the evaluation of the second parameter would introduce additional overhead.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using nose 1.1.2 to write tests for a Python project. There is this
I found that Python's assert statement is a good way to catch situations that
I'm doing TDD using Python and the unittest module. In NUnit you can Assert.Inconclusive(This
I came across this problem when attempting to learn python. Consider the following function:
I often use python's assert statement to check user input and fail-fast if we're
Consider this Python code: assert(a > 0) assert(b > 0) assert(a + b >
python 2.7 how do you take any number and be able to compress it
Python allows aliasing of imports, through ...as <ALIAS> clauses in the import statement, like
Python does not print traceback messages from exceptions raised in daemon threads. For example,
Python is so dynamic that it's not always clear what's going on in a

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.