Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   RE: remove assert statement (Was: Re: PEP new assert idiom) (http://www.velocityreviews.com/forums/t337854-re-remove-assert-statement-was-re-pep-new-assert-idiom.html)

Robert Brewer 11-07-2004 03:42 PM

RE: remove assert statement (Was: Re: PEP new assert idiom)
 
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
> 'assert' is always AssertionError. That is a major
> disadvantage, because
> it's not specific. If a method I'm calling would raise an
> AssertionError
> in 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 don't see the use of a statement like
> "assert isinstance(message, basestring)". It's by far the most common
> use of assert: some assertions about the types of the
> arguments. This is
> at best redundant. In the case of warnings.py, it's followed by
> re.compile(message, ...). Removing the assertion in this
> example would result
> in three differences:
> - The error raised would be TypeError, not
> AssertionError. Therefore
> it's more specific and thus easier to catch.
> - The error would me raised by the re module, not warnings module
> - The error would be "first argument must be string or compiled
> pattern" instead of "message must be a string". Therefore, the
> assertion makes it impossible to pass a compiled pattern instead
> of a string.
>
> Duck typing eliminates the use of assert.
>
> 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?


Whole-hearted +1. Let's get it into PEP 3000.

There are cases where isinstance is useful (as you point out, when you
wish to raise a more meaningful error of your own Exception subclass).
But you don't need an assert keyword for that either. ;)


Robert Brewer
MIS
Amor Ministries
fumanchu@amor.org

bsmith 11-07-2004 06:53 PM

RE: remove assert statement (Was: Re: PEP new assert idiom)
 
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


All times are GMT. The time now is 03:25 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.