On 5/17/20 5:04 PM, Alex Hall wrote:
> Some people (like myself, or the coworkers of [this 
> person](https://mail.python.org/archives/list/python-ideas@python.org/thread/PLXOXKACKGXN4ZKISDVXLKMFIETWTF63/))
>  just like to use asserts as a convenient way to check things. We don't want 
> asserts to ever be turned off. Maybe there could be some kind of compiler 
> directive which means "in this file, even with -O, keep the asserts". Maybe 
> the line `assert __debug__`?
>
My answer to that is 'Your doing int wrong', that is like asking for an
if statement to not bother checking some of its conditionl.

the assert operation has, like forever, been a debugging aid to say this
condition should ALWAYS/NEVER occur, check for it in Debug Builds, but
not for production builds. In some cases (some languages) the compiler
might even optimize to code based on the knowledge that the condition,
even in code executed before the condition (if it can be sure the assert
will be reached).

To quote the documentation:

Assertions should *not* be used to test for failure cases that can occur
because of bad user input or operating system/environment failures, such
as a file not being found. Instead, you should raise an exception, or
print an error message, or whatever is appropriate. One important reason
why assertions should only be used for self-tests of the program is that
assertions can be disabled at compile time.


Basically the statement

assert cond

is the same as

if __debug__ and not cond:

    raise AssertionError

If you really what what you describe, add the following to your code:

if not __debug__:

    raise AssertionError, "Please don't disable assertions"

(This won't enable the assertions, but will let the user know that you
need them enabled)

-- 
Richard Damon
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JO5EBPHA5IZMG37KNVXMYT5NE2Y4LHIM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to