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

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

Gabriel Genellina
Guest
Posts: n/a

 12-14-2008
En Sun, 14 Dec 2008 02:40:10 -0200, Benjamin Kaplan
<(E-Mail Removed)> escribió:

> On Sat, Dec 13, 2008 at 10:49 PM, Daniel Fetchinson <
> http://www.velocityreviews.com/forums/(E-Mail Removed)> 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

Rohannes
Guest
Posts: n/a

 12-14-2008
'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_i...on/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/tutor...operators.html

I guess it's a matter of context.

MRAB
Guest
Posts: n/a

 12-14-2008
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_i...on/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/tutor...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...