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 <vernondc...@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@python.org > http://mail.python.org/mailman/listinfo/edu-sig > -- Vern Ceder vce...@gmail.com, vce...@dogsinmotion.com The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW
_______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig