[Python-ideas] Raise exception if (not) true

Ryan Gonzalez rymg19 at gmail.com
Thu Feb 20 22:07:24 CET 2014


Wouldn't that cause problems if the exception specified takes more than one
parameter?

It's a nice idea, though. I can't believe I didn't think of that.


On Thu, Feb 20, 2014 at 2:44 PM, spir <denis.spir at gmail.com> wrote:

> On 02/20/2014 06:29 PM, MRAB wrote:
>
>> On 2014-02-20 17:10, Ryan Gonzalez wrote:
>>
>>> In Python, you'll constantly see code like this:
>>>
>>> ```python
>>> if x != y:
>>>      raise ValueError('x != y!!')
>>> ```
>>>
>>> or:
>>>
>>> ```python
>>> if not isinstance(x,SomeType):
>>>      raise TypeError('x is not SomeType!')
>>> ```
>>>
>>> Assertion help a bit:
>>>
>>> ```python
>>> assert isinstance(x,SomeType), 'x is not SomeType!'
>>> ```
>>>
>>> Notice I said "a bit". If optimizations are on, they're disabled. In
>>> addition, the only type of error thrown is an AssertionError.
>>>
>>> I propose a `raise_if` function. If the given condition is True, an
>>> exception is raised. So, the above examples would be shortened to:
>>>
>>> ```python
>>>
>>> raise_if(x!=y, ValueError, 'x != y!!')
>>> raise_if(not isinstance(x,SomeType),TypeError, 'x is not SomeType!')
>>>
>>> ```
>>>
>>> There could also be a raise_if_not function that does the opposite:
>>>
>>> ```python
>>> raise_if_not(isinstance(x,SomeType), TypeError, 'x is not SomeType!')
>>> ```
>>>
>>> Thoughts?
>>>
>>>  So:
>>
>> raise_if_not(isinstance(x, SomeType), TypeError, 'x is not SomeType!')
>>
>> is equivalent to:
>>
>> if not isinstance(x, SomeType): raise TypeError('x is not SomeType!')
>>
>> ?
>>
>
> And:
>
>     raise_if(x!=y, ValueError, 'x != y!!')
> is equivalent to:
>
>     if x != y: raise ValueError('x != y!!')
>
>  It doesn't improve the language much, IMHO! :-)
>>
>
> Ditto.
>
> But I would like to be able to add an error type to assertions (in
> addition to the optional message). This is particularly useful for people
> (like me) who systematically check func inputs (for client debugging
> comfort), using assert's. It would be mainly ValueError and TypeError.
>
> Example:
>     assert x > 0, "x should be positive", ValueError
> gives:
>     ValueError: x should be positive
> instead of:
>     AssertionError: x should be positive
>
> This is very similar to the proposal above, semantically and practically,
> except we are here just reusing the builtin 'assert' instruction with an
> additional parameter. (Seems backward-compatible to me, at first sight,
> provided the new param comes last. Maybe an issue is the hypothetical
> mention of an error type, without message.)
>
>
> d
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Ryan
If anybody ever asks me why I prefer C++ to C, my answer will be simple:
"It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was
nul-terminated."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140220/57911c8c/attachment.html>


More information about the Python-ideas mailing list