<p dir="ltr">Other than the fact that this would completely fail when run with -O...</p>
<p dir="ltr">I believe I brought this up a while back (~1-2 years), and that was the reply.</p>
<p dir="ltr">--<br>
Ryan<br>
[ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong.<br>
<a href="http://kirbyfan64.github.io/">http://kirbyfan64.github.io/</a></p>
<div class="gmail_quote">On May 2, 2016 9:23 AM, "Giampaolo Rodola'" <<a href="mailto:g.rodola@gmail.com">g.rodola@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>assert statement gives the possibility to display the text which goes along with the AssertionError exception. Most of the times though, what would be more appropriate is to raise a different exception (e.g. ValueError). My proposal is to be able to specify an exception as a replacement for AssertionError as in:</div><div><br>>>> assert callable(fun), ValueError("object is not a callable")</div><div>ValueError: object is not a callable<br></div><div><br></div><div>Specifically, this would be useful at the top of a function or method, where argument types or values are usually checked:<br><br><div>def retry(times=3, timeout=0.1, callback=None):</div><div> assert times >= 1, ValueError("times must be >= 1")</div><div> assert isinstance(timeout, (int, float)), ValueError("invalid timeout")</div><div> assert callable(callback), ValueError("callback is not a callable")</div></div><div><br></div><div>...as opposed to:</div><div><br></div><div><div>def retry(times=3, timeout=0.1, callback=None):</div><div> if not times >= 1:</div><div> raise ValueError("times must be >= 1")</div><div> if not isinstance(timeout, (int, float)):</div><div> raise ValueError("invalid timeout")</div><div> if not callable(callback):</div><div> raise ValueError("callback is not a callable")</div></div><div><br></div><div>Other than saving 1 line for each type/value check, this has the advantage that the assertion logic (e.g. "times >= 1") is shown in the traceback message itself, because it's on the same line, enriching the context and giving more information in case of error.</div><div><br></div><div>Thoughts?</div><div><br></div>-- <br><div><div dir="ltr"><div>Giampaolo - <a href="http://grodola.blogspot.com" target="_blank">http://grodola.blogspot.com</a></div><div><br></div></div></div>
</div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div>