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

kirby urner kirby.urner at gmail.com
Mon Dec 19 23:36:20 CET 2011


>
> if ext not in res_dict:
>     res_dict[ext] = 0
>
> res_dict[ext] += 1
>
> Cheers,
> Carl.

Truth be told, I do that too a lot.

After being shown all the options, let the programmer choose.

However, in a learning situation, it's about showing these options,
including with the dict.get method.

These patterns need not be visited in a vaccuum, i.e. it's quite often
that one needs a default returned (and not an error message) if such
and such is the case, a pre-existing value otherwise.

This pattern is not confined to dicts of course.

What I will correct though, as an example of what I consider a mistake, is like:

for word in wordlist:
    if word == "fox":
        result = True
    else:
        result = False

in place of

result = "fox" in wordlist

Note every alternative weighs equally.  Laura things  thedict [thekey]
= thedict.get( thekey, 0) + 1 is starting to get too fancy / clever /
tricky and detracts from readability (a shared value).

I would agree there are honest disagreements about what's "too clever"
(I think using a builtin method for what it's for should be shown, but
then possibly avoided).

However, coding a loop to go through a list looking for a match,
rather than letting the keyword "in" do the work, strikes me as "poor
Python" that should be tightened up to pass muster.

I don't think an "anything goes" attitude works.  Like in sports, some
moves win more points and when you hire a coach, it's to develop best
practices, not to prove how you're free of all idioms.

Kirby


More information about the Edu-sig mailing list