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

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

1. ### Gabriel GenellinaGuest

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

2. ### RohannesGuest

'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

3. ### MRABGuest

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

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