<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, May 2, 2016 at 8:38 AM, Ryan Gonzalez <span dir="ltr"><<a href="mailto:rymg19@gmail.com" target="_blank">rymg19@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><p dir="ltr">I feel like you just brought up the exact issue. Assertions should *never* be used for things like this. Because, one day, some stupid idiot is going to use assertions to perform some sort of data validation, someone else will use that library with -O, and the world will explode.</p>
<p dir="ltr"></p></blockquote><div>That's a very strong opinion, and a bit of a doomsday scenario. I hear worries about this a lot, but I've never heard a story from someone to whom this actually happened.<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><p dir="ltr">It's just far too attractive to use but far too easy to misuse.<br></p></blockquote><div>The thing is, assert exists and is not going away. People are writing
these asserts today (there are still many in asyncio). In many cases the
assert is not guarding against bad data or user input, but simply
against a confused caller. The assumption is that once the program is
debugged enough to go to production (where you use -O) the need for the
asserts has gone down enough that it doesn't matter the checks are gone
-- the confused call site has long been found during tests and fixed. <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><p dir="ltr">And, if you really want:</p>
<p dir="ltr">if my_cond: raise MyError('abc')</p>
<p dir="ltr">Compare that to:</p>
<p dir="ltr">assert my_cond, MyError('abc')</p></blockquote><div>There's a bug in your example, and that bug is a good argument for using assert: the equivalent of that assert should be<br><br></div><div> if not my_cond: raise MyError('abc')<br><br></div><div>and that extra inversion often makes the code a little less straightforward. Compare<br><br></div><div> if times < 1: raise ValueError()<br><br></div><div>to<br><br></div><div> assert times >= 1: raise ValueError()<br><br></div><div>The latter states positively what we're expecting. This is much more helpful for the reader.<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<p dir="ltr">Also, RPython uses assertions for error handling. Trust me, dealing with that is *not* fun.</p></blockquote><div><br>RPython is not Python; isn't that a feature? :-)<br></div></div><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>