[Python-ideas] Raise exception if (not) true
Markus Unterwaditzer
markus at unterwaditzer.net
Thu Feb 20 19:21:04 CET 2014
On 2014-02-20 18:10, Ryan Gonzalez wrote:
> 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?
I don't see what is wrong with using `if`-statements. They're IMO more
readable and obviously they have more usecases.
-- Markus
More information about the Python-ideas
mailing list