<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Tue, Nov 28, 2017 at 6:08 PM Ivan Pozdeev via Python-ideas <<a href="mailto:python-ideas@python.org">python-ideas@python.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 28.11.2017 16:36, Nick Coghlan wrote:<br>
<br>
> On 28 November 2017 at 15:41, Steven D'Aprano <<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>> wrote:<br>
>> On Tue, Nov 28, 2017 at 05:12:36AM +0300, Ivan Pozdeev via Python-ideas wrote:<br>
>>> Unlike C, Python does the aforementioned checks all the time, i.e. it's<br>
>>> effectively always in "debug mode".<br>
>> Apart from -O which disables assertions. But in any case, the best use<br>
>> of assertions is not checking things which the interpreter is going to<br>
>> do anyway, but checking things which the interpreter can not and does<br>
>> not check automatically: your program logic. There is no way that the<br>
>> Python interpreter is going to do this check automatically, unless I<br>
>> write the assertion:<br>
>><br>
>> assert 0 <= r < abs(y)<br>
>><br>
>> That is copied straight out of one of my functions.<br>
> I'll make the same observation I usually do each time one of these<br>
> threads comes up:<br>
><br>
> * I'm opposed to making assert substantially different from the way it works now<br>
> * I'm in favour of adding a new "ensure()" builtin that encapsulates<br>
> the check-and-raise logic<br>
><br>
> The reasons I prefer this approach:<br>
><br>
> - assert is a statement *solely* so that the compiler can optimise it<br>
> out. If it's not optional,<br>
>    it doesn't need to be a statement any more<br>
Another benefit of a statement vs function is only evaluating the<br>
error-related arguments when there's an error<br></blockquote><div><br></div><div>I'm not sure what the use case is, but it could be implemented easily as</div><div><br></div><div>    ensure(not hasattr(e, "exception")) or raise e.exception</div><div><br></div><div>... had "raise" been an expression, an idea repeatedly rejected here. It's still implementable with a "throw()" function.</div><div><br></div><div>Elazar </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> - if the existing assert statements are left alone, there are no<br>
> performance or compatibility<br>
>    concerns for folks that rely on the current behaviour<br>
> - if it's a function, it doesn't need to be called "assert", it can use the more<br>
>    imperative term "ensure" (meaning "ensure this condition is true<br>
> before continuing")<br>
> - if it's a function, it can easily be emulated on old versions via<br>
> compatibility libraries<br>
> - "ensure() is required, assert is optional" is a better answer to<br>
> complaints about<br>
>    assertions being optional than suggesting "if cond: raise AssertionError(msg)"<br>
>    as a reasonable alternative to "assert cond, msg"<br>
> - if it's a function, we get access to all the regular function<br>
> machinery, so we're<br>
>    not restricted to positional-only arguments the way the assert statement is<br>
><br>
> My initial proposed behaviour for the function:<br>
><br>
>      def ensure(cond, msg=None, exc_type=RuntimeError):<br>
>          """Raise an exception if the given condition is not true"""<br>
>          if not cond:<br>
>              if msg is None:<br>
>                  frame = sys._getframe(1)<br>
>                  line = frame.f_lineno<br>
>                  modname = frame.f_globals.get("__name__", "<unknown module>")<br>
>                  msg = f"Condition not met on line {line:d} in {modname!r}"<br>
>              raise exc_type(msg)<br>
><br>
> Cheers,<br>
> Nick.<br>
><br>
> P.S. No, I'm not offering to write that PEP myself, I'd just be in<br>
> favour of the idea if someone else were to write it :)<br>
><br>
<br>
--<br>
Regards,<br>
Ivan<br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">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></div>