A little disappointed so far
Ian Bicking
ianb at colorstudy.com
Mon May 19 03:58:49 EDT 2003
On Mon, 2003-05-19 at 02:38, Alex Martelli wrote:
> >> open ($file) || die "Couldn't open $file"
> >> strikes me as rather readable. And concise.
> >
> > In Python, just write:
> > f = open(file)
> >
> > If there is an exception, an error message will be generated explaining
> > why the file could not be opened and your script will terminate.
> >
> > The problem with your Perl code is that it doesn't really help with
> > problem diagnosis i.e. does the file not exist, is it a directory, do I
> > not have the necessary permissions, etc.
>
> That's just because the die statement's argument above isn't using the
> normal idiomatic Perl form, which would be:
>
> die "Couldn't open data file $file: $!"
>
> where the $! gives the details of the error. Not Perl's fault for
> once -- it's just as possible to erroneously and anti-idiomatically
> omit printing the error details in a Python except clause.
You have to go to more trouble with the Python, like:
try:
f = open(file)
except IOError, e:
print "Couldn't open %s" % file
# Equivalent improvement:
# print "Couldn't open %s: %s" % (file, e)
If you don't try to handle the error yourself, you get a long-winded and
distracting traceback, but you also get the appropriate message. The
"idiom" for primitive error handling is nil -- don't do anything, and
errors happen loudly and with some diagnostic information. So I think
it *is* Perl's fault that doing even primitive error handling requires
an idiom.
But arguing on Perl's flaws is probably silly in a Python group...
Ian
More information about the Python-list
mailing list