Re: [Tutor] Newbie Question on Exceptions...

2007-05-08 Thread John Fouhy
On 09/05/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> try:
> print "The fridge contains %s" %fridge[food_sought]
> except (KeyError):
> print "The fridge does not contain %s"%food_sought
[...]
> Is the same true of Python? Or is ok to use Exception handling like the book 
> suggests?

This general debate is called "look before you leap" vs "easier to ask
forgiveness than permission".  If you google for "python eafp lbyl"
you will get a zillion pages of people debating it.  Here's a quote
from a post by Alex Martelli
(http://en.wikipedia.org/wiki/Alex_Martelli):

# from http://mail.python.org/pipermail/python-list/2003-May/205182.html
"""
There are umpteen good reasons why EAFP is vastly superior to LBYL.  For
example, we can just focus on the fact that these days we work mostly on
multiprogrammed machines.  While script A is running, some other programs
B, C, D, ... are typically also running -- and they might be mucking
with the same directories and/or files that A is working with.

So, if A's structure is:

if iswhatiwant(thefile):
useappropriately(thefile)
else:
dealwithwrongness()

then A is buggy.  That is because, between the moment in which the test
'iswhatiwant' runs (and returns a true value), and the later moment in
which procedure 'useappropriately' runs, *just about anything may have
happened* -- in particular, some other program Z might have removed or
modified 'thefile' so that it's NOT what A wants any more.  I.e., A may
lose control of the CPU between the moment it tests and the later time
in which it uses the result of that test.

This is known as a "race condition" and it's among the hardest problems
you may run into.  A may seem to be running just fine 99 times and then
the 100th time BOOM -- because of accidents of timing between A and
other stuff that may be running "at the same time"... a "race", so to
speak, whence the name whereby this horrid condition is known.

Fortunately, in a language with good support for exceptions such as
Python, you are NOT doomed to enter the hell of race conditions -- just
use EAFP instead of LBYL:

try:
useappropriately(thefile)
except ItWasWrong, howwasitwrong:
dealwithwrongness()

See how deeply simpler this is?  'useappropriately' just ASSUMES the
file is 'what A wants' and raises an ItWasWrong exception if the
assumption proves to be unfounded.  You don't have to code a
separate 'iswhatiwant' test -- what you DO want is determined inherently
by what 'useappropriately' tries to do.  No race conditions, no code
that must duplicate the set of conditions to be checked for, no
duplicate work at runtime in terms of system calls to determine
if a condition holds followed by system calls to take advantaqe
of that condition.

This risks leaving the impression that EAFP is a panacea - it isn't,
and it has its own issues to watch for -- it's simply heads and
shoulders above LBYL in most practical cases.  Please see my more
detailed discussions of this in the Cookbook and the Nutshell for
something more about error-checking strategies.
"""

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie Question on Exceptions...

2007-05-08 Thread Alan Gauld
<[EMAIL PROTECTED]> wrote

> fridge={"apple":"A shiny red apple","pear":"a nice ripe 
> pear","grapes":"seadless grapes"}
> food_sought="apple"
> fridge_list=fridge.keys();

Not sure what this line is for...

> try:
>print "The fridge contains %s" %fridge[food_sought]
> except (KeyError):
>print "The fridge does not contain %s"%food_sought

This is a fairly common Python idiom.

> I'm fairly certain the book is in error in calling this a 
> "short-cut"

I agree, its not much of a shortcut. but oit is a common Python idiom.

> In Java using exceptions in the way shown above is
> a classic anti-pattern since Exceptions should only
> be used for..well exceptional conditions.

There are a few reasons for this in Jave, not least that
Exceptions are quite expensive in Java whereas they
are relatively cheap in Python.

> Or is ok to use Exception handling like the book suggests?

Its generally OK but at the same time don't overdo it.
Exceptions have a lot in common with gotos. They can
obfuscate the flow of code unless the try block is short
and simple. Personally I prefer to use exception for real
exceptions when possible, but sometimes they do offer
a neat way of expressing things. And of course accessing
a non existent key is an exception!

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie Question on Exceptions...

2007-05-08 Thread Andre Engels
2007/5/8, [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
> I'm working my way through the book "beginning python" and I came across an 
> exercise that suggests using Exception trapping to see if a value is in a 
> dictionary:
>
> fridge={"apple":"A shiny red apple","pear":"a nice ripe 
> pear","grapes":"seadless grapes"}
> food_sought="apple"
> fridge_list=fridge.keys();
> try:
> print "The fridge contains %s" %fridge[food_sought]
> except (KeyError):
> print "The fridge does not contain %s"%food_sought
>
> I'm fairly certain the book is in error in calling this a "short-cut" since 
> the has_key method is much less verbose to use,

Is it?

if fridge.has_key(food_sought):
foo
else:
bar

doesn't look much less verbose than:

try:
foo
except (KeyError):
bar

> but it brings up a question about exceptions in general:
>
> In Java using exceptions in the way shown above is a classic anti-pattern 
> since Exceptions should only be used for..well exceptional conditions.
>
> Is the same true of Python? Or is ok to use Exception handling like the book 
> suggests?

Exceptions are in general much more freely used in Python than in most
other languages, it's called the "EAFP" (It's easier to ask
forgiveness than to get permission) style, instead of the "LBYL" (look
before you leap) style most other languages use.


-- 
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie Question on Exceptions...

2007-05-08 Thread Bob Gailer
[EMAIL PROTECTED] wrote:
> I'm working my way through the book "beginning python" and I came across an 
> exercise that suggests using Exception trapping to see if a value is in a 
> dictionary:
>
> fridge={"apple":"A shiny red apple","pear":"a nice ripe 
> pear","grapes":"seadless grapes"}
> food_sought="apple"
> fridge_list=fridge.keys();
> try:
> print "The fridge contains %s" %fridge[food_sought]
> except (KeyError):
> print "The fridge does not contain %s"%food_sought
>
> I'm fairly certain the book is in error in calling this a "short-cut" since 
> the has_key method is much less verbose to use
Perhaps the version of Python, when the book was written, did not have 
has_key?
Less verbose? Let's see - if I do a straightforward translation I get:

if fridge.has_key(food_sought):
print "The fridge contains %s" %fridge[food_sought]
else:
print "The fridge does not contain %s"%food_sought

That's 2 less words!

But consider (same word count but even easier to read):

if food_sought in fridge:

> question about exceptions in general:
>
> In Java using exceptions in the way shown above is a classic anti-pattern 
> since Exceptions should only be used for..well exceptional conditions.  
>   
Ah the dreaded "should". Who says? But then in Java exception handling 
is more complex, and avoiding it seems a good idea.
> Is the same true of Python? Or is ok to use Exception handling like the book 
> suggests?
>   
Since there is no one on the Python side saying "should" (AFAIK) I can 
only opine: use whatever gets the job done, is readable and maintainable.

Many things can be tested for with ease. But consider when you use 
raw_input to get a string, and you want to accept it only if it will 
convert to float. The only easy way I know is to use the float() 
function inside a try:. If you wanted to test it without try: you'd have 
to write a regular expression for floating point syntax and use re. Not 
as easy or readable.

-- 
Bob Gailer
510-978-4454

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Newbie Question on Exceptions...

2007-05-08 Thread dsh0105
I'm working my way through the book "beginning python" and I came across an 
exercise that suggests using Exception trapping to see if a value is in a 
dictionary:

fridge={"apple":"A shiny red apple","pear":"a nice ripe 
pear","grapes":"seadless grapes"}
food_sought="apple"
fridge_list=fridge.keys();
try:
print "The fridge contains %s" %fridge[food_sought]
except (KeyError):
print "The fridge does not contain %s"%food_sought

I'm fairly certain the book is in error in calling this a "short-cut" since the 
has_key method is much less verbose to use, but it brings up a question about 
exceptions in general:

In Java using exceptions in the way shown above is a classic anti-pattern since 
Exceptions should only be used for..well exceptional conditions.  

Is the same true of Python? Or is ok to use Exception handling like the book 
suggests?

Thanks in advance,

David Hamilton
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor