remove assert statement (Was: Re: PEP new assert idiom)

John Roth newsgroups at jhrothjr.com
Sun Nov 7 13:37:52 EST 2004


"Gerrit" <gerrit at nl.linux.org> wrote in message 
news:mailman.6055.1099832572.5135.python-list at python.org...
> Paul Rubin wrote:
>> "Raymond Hettinger" <vze4rx4y at verizon.net> writes:
>> > Why do you need a statement for this?
>> > IMO, a plain function will do the trick:
>>
>> I do write such functions sometimes, but using a statement is more
>> natural.  That's why the assert statement exists, for example.
>
> I think 'assert' being a statement is on Guido's regrets list. exec and
> print are on his regrets list: both should have been a function. I
> think the same holds for assert, although I'm not sure.
>
>> statement is very great, and I find myself doing it all the time
>> thinking I'll clean it up later.  So I think having a statement for
>> runtime checking would be in the correct spirit.
>
> In my opinion, assert is almost useless. It can sometimes be useful for
> debugging purposes, but beyond that, it isn't. The exception raised by
> 'assert' is always AssertionError. That is a major disadvantage, because
> it's not specific. If a method I'm calling would raise an AssertionError
> is some cases, and I want to catch it, I'm catching too much. Creating a
> new exception-class and raising that one is better because of this.

I see assert as a statement of an invariant: each and every time
I come here this *must* be true. I don't see it as a statement
of wishful thinking: when I come here, this should be true or
I'm going to have problems.

For me, an assert failing is a programing error in the caller,
not in the method with the assert! Trying to catch an
assert is a misuse of what the statement provides.

Besides which, you can always dress up an assert
with a string that can be printed, and then further
stick variables into that string with %.

John Roth





More information about the Python-list mailing list