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

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

1. ### Daniel FetchinsonGuest

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

2. ### rGuest

> 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

3. ### Peter OttenGuest

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
4. ### Bruno DesthuilliersGuest

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

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
6. ### Tim RoweGuest

Unfortunately,

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

> True

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

--
Tim Rowe

Tim Rowe, Dec 15, 2008