[Edu-sig] Using try / except: any stipulations against routine use?

Vernon Cole vernondcole at gmail.com
Wed Dec 14 22:42:18 CET 2011


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


More information about the Edu-sig mailing list