[Edu-sig] Using try / except: any stipulations against routine use?
Bert Freudenberg
bert at freudenbergs.de
Wed Dec 14 23:15:17 CET 2011
On 14.12.2011, at 22:42, Vernon Cole 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
Isn't this at least as readable, and conceptually simpler?
if ext in res_dict:
res_dict[ext] += 1
else:
res_dict[ext] = 1
Not arguing against exceptions in general, but in this case?
- Bert -
More information about the Edu-sig
mailing list