[Edu-sig] Considering Idioms

kirby urner kirby.urner at gmail.com
Tue Apr 22 01:59:08 CEST 2008


Dylan and I had some interesting chatter on the PPUG list
(formerly PORPIG), regarding what I advanced as the
David Goodger thesis (based on last year's OSCON
tutorial), that it's more idiomatic to say "for e in thedict:"
than "for e in thedict.keys():"

The reasoning here (as I understood it) is thedict is an
iterable and so has a __next__, meaning we don't need
to flag it as anything special with the keys() modifier, i.e.
stick with a more generic syntax to where the reader is
just thinking "iterable" and not "dictionary" per se.

Dylan points out that you might want to clue the reader
that dictionaries are a special kind of iterable in having
no specific sequence i.e. you can't get at its successive
keys with integer subscripting, unlike with lists, tuples,
and strings.

Importantly, then, the concept of "iterable" is such that
we may have no way of "jumping ahead" i.e. we can go
next, next, next, but there's no way to subscript i.e.
bypass the intermediate steps, to get to some distant
element in the iteration.

We have this in mathematics a lot:  sequences with
clear rules for getting the next one, but no closed form
expression for getting the nth, short of computing all
intermediate steps (cellular automata studies furnish
any number of examples).

Kirby

** http://mail.python.org/pipermail/portland/2008-April/000327.html


More information about the Edu-sig mailing list