Re: 1 or 1/0 doesn't raise an exception

Discussion in 'Python' started by Daniel Fetchinson, Dec 14, 2008.

  1. >> Is it a feature that
    >>
    >> 1 or 1/0
    >>
    >> returns 1 and doesn't raise a ZeroDivisionError? If so, what's the
    >> rationale?

    >
    > Yes, it's a feature:
    >
    > http://en.wikipedia.org/wiki/Short-circuit_evaluation
    >
    > When you have "True or False", you know it's true by the time
    > you've got the first piece, so there's no need to evaluate the
    > 2nd piece. The opposite is helpful too:
    >
    > lst = [some list or an empty list]
    > ...
    > if lst and lst[0] == 42:
    >
    > This ensures that the "lst[0]" doesn't fail if lst is empty,
    > because lst evaluating to false (an empty list) short-circuits
    > preventing the evaluation of "lst[0]".


    Okay, it's clear, thanks.

    Let me just point out that unsuspecting people (like me) might rely on
    the whole expression to be evaluated and rely on exceptions being
    raised if needed.

    So from now on I will not do!

    --
    Psss, psss, put it down! - http://www.cafepress.com/putitdown
    Daniel Fetchinson, Dec 14, 2008
    #1
    1. Advertising

  2. Daniel Fetchinson

    r Guest

    > Let me just point out that unsuspecting people (like me) might rely on
    > the whole expression to be evaluated and rely on exceptions being
    > raised if needed.


    This happens when people assume something ;)
    Use a different construct if you want to catch error's, I don't
    understand how you could not get it?

    >>> bool(1)

    True
    >>> bool(0)

    False
    >>> bool([])

    False
    >>> bool([0])

    True
    >>> bool([0,0,0])

    True
    >>> bool({})

    False
    >>> bool(())

    False
    see a pattern here?
    >>> [] or {} or () or 0 or 1

    1
    >>> bool('python rules!')

    True
    r, Dec 14, 2008
    #2
    1. Advertising

  3. Daniel Fetchinson

    Peter Otten Guest

    Grant Edwards wrote:

    > Short circuit evaluation of booleans is very common (and has
    > been for decades), so I don't know why people would expect
    > something else.


    Visual Basic ;)
    Peter Otten, Dec 14, 2008
    #3
  4. Grant Edwards a écrit :
    > On 2008-12-14, Daniel Fetchinson <> wrote:
    >
    >> Let me just point out that unsuspecting people (like me) might rely on
    >> the whole expression to be evaluated and rely on exceptions being
    >> raised if needed.

    >
    > Short circuit evaluation of booleans is very common (and has
    > been for decades), so I don't know why people would expect
    > something else.
    >

    Because they either have no previous programming experience, or only
    experience with one of the few languages that don't do short-circuit
    evaluation ?

    <OP>
    You can consider that short-circuit is the norm and it's absence the
    exception.
    </OP>
    Bruno Desthuilliers, Dec 14, 2008
    #4
  5. Daniel Fetchinson

    Mel Guest

    Daniel Fetchinson wrote:
    [ ... ]
    > Let me just point out that unsuspecting people (like me) might rely on
    > the whole expression to be evaluated and rely on exceptions being
    > raised if needed.


    There are a lot of threads on comp.lang.python that mention beginners'
    possible reactions to language features, so I'll mention this paper
    relating to teaching programming:

    <http://www.cs.mdx.ac.uk/research/PhDArea/saeed/>.
     
    The early draft which I've read,

    <http://www.cs.mdx.ac.uk/research/PhDArea/saeed/paper1.pdf>,

    references quite a lot of literature on what new programmers do, discusses a
    new test for programming aptitude.

            Mel.
    Mel, Dec 14, 2008
    #5
  6. Daniel Fetchinson

    Tim Rowe Guest

    Unfortunately,

    >>>> bool('Ruby totally pwn3s Python!')

    > True


    Using Python is not total protection against buggy programs ;-)


    --
    Tim Rowe
    Tim Rowe, Dec 15, 2008
    #6
    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. Bengt Richter
    Replies:
    0
    Views:
    346
    Bengt Richter
    Jul 7, 2005
  2. Jacol

    raise or not to raise [Newbie]

    Jacol, Feb 3, 2007, in forum: Python
    Replies:
    5
    Views:
    398
    Gabriel Genellina
    Feb 5, 2007
  3. ernest
    Replies:
    2
    Views:
    275
    Roy Smith
    Nov 14, 2010
  4. Jack Bates
    Replies:
    0
    Views:
    267
    Jack Bates
    May 2, 2011
  5. bvdp

    Raise X or Raise X()?

    bvdp, Mar 11, 2012, in forum: Python
    Replies:
    10
    Views:
    351
    Stefan Behnel
    Mar 12, 2012
Loading...

Share This Page