[Edu-sig] Using try / except: any stipulations against routine use?
Vern Ceder
vceder at gmail.com
Wed Dec 14 23:06:10 CET 2011
I think I'd go along with Laura and Vernon as long as it's made clear that
one should specify the exception, as Vernon does in his sample. Bare
excepts should NOT be encouraged, particularly since I've seen some
students find them way too seductive - "Look, I can handle EVERY error!" ;)
OTOH the .get() method has always struck me as imposing a bit of a
cognitive load that I could live without.
Vern
On Wed, Dec 14, 2011 at 3:42 PM, Vernon Cole <vernondcole at gmail.com> wrote:
> Sorry, Kirby, I'm afraid I disagree.
> try:
> res_dict[ext] += 1
> except KeyError:
> res_dict[ext] = 1
> is clear in intent and operation. It is a self-initializing occurrence
> counter.
>
> On the other hand,
> res_dict[ext] = res_dict.get(ext, 0) + 1
> is obscure. Unless I go dig up the documentation of what the .get()
> operation does on a dictionary, I have no idea what the intention of
> the code is -- and I am (humbly) a very experienced programmer and
> Python programmer. Also I doubt whether the one-line version is
> faster. Without looking at the code, I would bet that .get() is
> implemented using a try / except structure. There is nothing wrong
> with that.
>
> Remember the Zen of Python:
> >>>import this
> ...
> Readability counts.
> ...
> --
> Vernon
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>
--
Vern Ceder
vceder at gmail.com, vceder at dogsinmotion.com
The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20111214/425da729/attachment.html>
More information about the Edu-sig
mailing list