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

bsmith bsmith at aon.at
Sun Nov 7 13:53:08 EST 2004


On Sun, 07 Nov 2004 07:42:21 -0800, Robert Brewer wrote:

> Gerrit wrote:
>> 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

Yea, that's what assertions are for!  They allow the programmer to
document their assumptions in such a way that they'll be immediately
notified of a collision between their mental model and the real world. 

If you want more specificity, may I suggest explicitly throwing an
Exception.  I suppose this is one of the reasons why using assertions to
check preconditions of routines is frowned upon in some quarters.  To
enforce necessary preconditions of a function (which are part of the
function's public interface) it makes more sense to use exceptions
specifically defined for that purpose.

>> 
>> Duck typing eliminates the use of assert.

Duck typing has nothing to do with assertions.  


>> 
>> In my opinion, assert should be deprecated and then removed in Py3K:
>> assertions are redundant, unspecific, and conflict with the philosophy
>> of duck typing and EAFP.
>> 
>> What do others think of this?

I couldn't disagree more.  

The problem here isn't 'assert', but rather
it's misapplication.  Use assertions to check your own assumptions (i.e.
internally); use explicit checks and Exceptions to check inputs coming in
through your public interfaces.

I for one find assertions very useful in checking my own work, as it were.

// Ben



More information about the Python-list mailing list