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
On Mon, Apr 21, 2008 at 4:59 PM, kirby urner <kirby.urner@gmail.com> wrote: << SNIP >>
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.
Note that in 2.x there's no __next__ method per se i.e. no under-under "rib" (special name). This changed with PEP 3114: http://www.python.org/dev/peps/pep-3114/ Kirby
participants (1)
-
kirby urner