Making assert raise a different exception type

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.

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@cool-rr.com> wrote:
-- --Guido van Rossum (home page: http://www.python.org/~guido/)

On Fri, Oct 16, 2009 at 09:52:49AM -0700, Guido van Rossum wrote:
if not some_false_condition:
if __debug__ and not some_false_condition... Oleg. -- Oleg Broytman http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.

On Sat, 17 Oct 2009 04:03:38 am Oleg Broytman wrote:
That's not quite the same as assert. With assert, the test of __debug__ happens at compile time, not runtime. With the above, it happens at runtime (at least in Python 2.6). To get the same behaviour as assert, you would need something like: if __debug__: if not some_condition: raise ValueError(msg) -- Steven D'Aprano

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@cool-rr.com> wrote:
-- --Guido van Rossum (home page: http://www.python.org/~guido/)

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@cool-rr.com> wrote:
-- --Guido van Rossum (home page: http://www.python.org/~guido/)

On Fri, Oct 16, 2009 at 09:52:49AM -0700, Guido van Rossum wrote:
if not some_false_condition:
if __debug__ and not some_false_condition... Oleg. -- Oleg Broytman http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.

On Sat, 17 Oct 2009 04:03:38 am Oleg Broytman wrote:
That's not quite the same as assert. With assert, the test of __debug__ happens at compile time, not runtime. With the above, it happens at runtime (at least in Python 2.6). To get the same behaviour as assert, you would need something like: if __debug__: if not some_condition: raise ValueError(msg) -- Steven D'Aprano

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@cool-rr.com> wrote:
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (4)
-
cool-RR
-
Guido van Rossum
-
Oleg Broytman
-
Steven D'Aprano