Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
 
Daniel Fetchinson
Guest
Posts: n/a
 
      12-14-2008
>> 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
 
Reply With Quote
 
 
 
 
r
Guest
Posts: n/a
 
      12-14-2008
> 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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-14-2008
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

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-14-2008
Grant Edwards a Úcrit :
> On 2008-12-14, Daniel Fetchinson <(E-Mail Removed)> 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>
 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      12-14-2008
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.
 
Reply With Quote
 
Tim Rowe
Guest
Posts: n/a
 
      12-15-2008
Unfortunately,

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

> True


Using Python is not total protection against buggy programs


--
Tim Rowe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Raise X or Raise X()? bvdp Python 10 03-12-2012 04:08 PM
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
raise Exception or raise Exception() ernest Python 2 11-14-2010 08:14 PM
raise or not to raise [Newbie] Jacol Python 5 02-05-2007 11:46 PM
method for Exception base class to raise the exception in an expression? Bengt Richter Python 0 07-07-2005 05:07 PM



Advertisments