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

Discussion in 'Python' started by Robert Brewer, Nov 7, 2004.

  1. 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
    Robert Brewer, Nov 7, 2004
    #1
    1. Advertising

  2. Robert Brewer

    bsmith Guest

    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
    bsmith, Nov 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christoph Becker-Freyseng

    PEP for new modules (I read PEP 2)

    Christoph Becker-Freyseng, Jan 15, 2004, in forum: Python
    Replies:
    3
    Views:
    370
    Gerrit Holl
    Jan 16, 2004
  2. =?ISO-8859-1?Q?F=E1bio?= Mendes

    PEP new assert idiom

    =?ISO-8859-1?Q?F=E1bio?= Mendes, Nov 6, 2004, in forum: Python
    Replies:
    28
    Views:
    746
    Raymond Hettinger
    Nov 8, 2004
  3. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,520
    Carl Banks
    Feb 23, 2005
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    915
    Keith Thompson
    Nov 25, 2004
  5. Kenneth McDonald

    Is there a standard "assert" idiom in Ruby?

    Kenneth McDonald, Aug 2, 2007, in forum: Ruby
    Replies:
    8
    Views:
    159
    John Carter
    Aug 3, 2007
Loading...

Share This Page