[Tutor] Readability: To use certain Python features or not?

boB Stepp robertvstepp at gmail.com
Sun Jun 27 18:21:14 EDT 2021


On Sun, Jun 27, 2021 at 4:49 PM <alan.gauld at yahoo.co.uk> wrote:
>
> Personally I think the second is more reliable and maintainable so prefer it. If a reader is so new to python they don't know about get() then they need to look it up and learn. But OTOH a default dict might be better still!
> There is a difference between writing clever code that is unreadable and using standard language or library features that might be less well known. One is easy to look up, the other is just downright hard work and therefore fragile.
>
> On 27 Jun 2021 22:41, boB Stepp <robertvstepp at gmail.com> wrote:
>
> Questions inspired by an example from "Practical Programming, 3rd ed."
> by Gries, Campbell and Montojo.
>
> p. 221 example.  Compare:
>
> [...]
> bird_to_observations = {}
> for line in observations_file:
>     bird = line.strip()
>     if bird in bird_to_observations:
>         bird_to_observations[bird] = bird_to_obserations[bird] + 1
>     else:
>         bird_to_observations[bird] = 1
> [...]
>
> to
>
> [...]
> bird_to_observations = {}
> for line in observations_file:
>     bird = line.strip()
>     bird_to_observations[bird] = bird_to_observations.get(bird, 0) + 1
> [...]

Hmm.  So, Alan, I guess you are suggesting the following for this
concrete instance:

from collections import defaultdict
[...]
bird_to_obeservations = defaultdict(int)
for line in observations_file:
    bird = line.strip()
    bird_to_observations[bird] += 1
[...]

That does look to my eye clearer and more expressive.  The cognitive
load on the reader is to know how to use default dictionaries and know
that int() always returns 0.  But as you point out the reader can
always look up defaultdict and the collections module is very popular
and well-used AFAIK.

So if I am understanding your answer to the more general questions,
you believe that even using less well-known Python standard features
is desirable if it simplifies the code presentation and is more
expressive of intent?

Cheers!
boB Stepp


More information about the Tutor mailing list