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

Discussion in 'Python' started by Gabriel Genellina, Dec 14, 2008.

  1. En Sun, 14 Dec 2008 02:40:10 -0200, Benjamin Kaplan
    <> escribió:

    > On Sat, Dec 13, 2008 at 10:49 PM, Daniel Fetchinson <
    > > wrote:
    >
    >> >> Is it a feature that
    >> >>
    >> >> 1 or 1/0
    >> >>
    >> >> returns 1 and doesn't raise a ZeroDivisionError? If so, what's the
    >> >> rationale?
    >> >
    >> > http://en.wikipedia.org/wiki/Short-circuit_evaluation

    >>
    >> 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.

    >
    > If you want both expressions evaluated, you can use & and |, just like
    > in C
    > and Java (&& and || are used for short circuit evaluation in those
    > languages).


    No: &, | (and ^, too) perform bitwise operations in Python, C and Java:

    py> 1 & 2
    0

    && and || --in both C and Java-- are like `and` and `or` in Python; they
    perform logical operations, and short-circuit evaluation of their operands.
    If you want to evaluate a logical expression without short circuiting, do
    that explicitely:

    a = first part
    b = second part
    if a or b: ...

    --
    Gabriel Genellina
    Gabriel Genellina, Dec 14, 2008
    #1
    1. Advertising

  2. Gabriel Genellina

    Rohannes Guest

    'Dive into Python' has a very memorable and interesting section on the
    exact behaviour of 'and' and 'or' in Python:

    http://diveintopython.org/power_of_introspection/and_or.html

    > No: &, | (and ^, too) perform bitwise operations in Python, C and Java:


    "In complete evaluation ... both expressions are always evaluated. To
    obtain complete evaluation in Java, you use & rather than && ... and
    use | in place of ||" - Walter Savitch, Absolute Java 2nd ed.

    Although & IS the bitwise-AND operator in Java:
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

    I guess it's a matter of context.
    Rohannes, Dec 14, 2008
    #2
    1. Advertising

  3. Gabriel Genellina

    MRAB Guest

    Rohannes wrote:
    > 'Dive into Python' has a very memorable and interesting section on the
    > exact behaviour of 'and' and 'or' in Python:
    >
    > http://diveintopython.org/power_of_introspection/and_or.html
    >
    >> No: &, | (and ^, too) perform bitwise operations in Python, C and Java:

    >
    > "In complete evaluation ... both expressions are always evaluated. To
    > obtain complete evaluation in Java, you use & rather than && ... and
    > use | in place of ||" - Walter Savitch, Absolute Java 2nd ed.
    >
    > Although & IS the bitwise-AND operator in Java:
    > http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
    >
    > I guess it's a matter of context.
    >

    Java has a bool type that's distinct from the int type. &, | and ^ are
    bitwise for int and Boolean for bool, and in both cases they are strict.
    && and || are for bool only and they are lazy.

    In Pascal the behaviour of "and" and "or" is implementation-dependant...
    MRAB, Dec 14, 2008
    #3
    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:
    341
    Bengt Richter
    Jul 7, 2005
  2. Jacol

    raise or not to raise [Newbie]

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

    Raise X or Raise X()?

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

Share This Page