[Python-ideas] Making assert raise a different exception type

Guido van Rossum guido at python.org
Fri Oct 16 19:15:27 CEST 2009


Sounds like a pretty odd use case to me. If the error is so common
that you care about which exception is raised an assert is probably
the wrong thing (and ignoring it with -O doubly so). Note that you can
put an arbitrarily complex expression resulting in a string as the
second argument, so you can make the message as pretty and informative
as you want -- it'll just be prefixed with AssertionError: so that
it's still clear this is a "should not happen" error.

On Fri, Oct 16, 2009 at 10:08 AM, cool-RR <cool-rr at cool-rr.com> wrote:
> Right. The reason I want it in an `assert` is to make use of the `-o` tag.
> And I would prefer to raise a more informative exception than
> AssertionError.
>
> On Fri, Oct 16, 2009 at 6:52 PM, Guido van Rossum <guido at python.org> wrote:
>>
>> I don't think so. You can already write
>>
>> if not some_false_condition:
>>  raise ValueError("Message")
>>
>> On Fri, Oct 16, 2009 at 9:16 AM, cool-RR <cool-rr at cool-rr.com> wrote:
>> > Hello!
>> >
>> > I would have liked to do something like this:
>> >
>> > assert some_false_condition, ValueError("Message")
>> >
>> > Meaning that if the assertion fails, the ValueError is raised instead of
>> > AssertionError. Do you think it's something that has a chance of being
>> > implemented? If so, I will write a PEP.
>> >
>> > Ram.
>> >
>> > _______________________________________________
>> > Python-ideas mailing list
>> > Python-ideas at python.org
>> > http://mail.python.org/mailman/listinfo/python-ideas
>> >
>> >
>>
>>
>>
>> --
>> --Guido van Rossum (home page: http://www.python.org/~guido/)
>
>
>
> --
> Sincerely,
> Ram Rachum
>



-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-ideas mailing list