Python Iterables struggling using map() built-in
Chris Angelico
rosuav at gmail.com
Sun Dec 7 19:34:23 EST 2014
On Mon, Dec 8, 2014 at 11:12 AM, Roy Smith <roy at panix.com> wrote:
> Ugh. When I see "while foo", my brain says, "OK, you're about to see a
> loop which is controlled by the value of foo being changed inside the
> loop". That's not at all what's happening here, so my brain runs into a
> wall.
I agree, with the caveat that this kind of thing makes a fine infinite loop:
while "No exception raised":
# do stuff that can raise an exception
while "Playing more games":
play_game()
if not still_playing: break
reset_game_board()
Nobody expects a string literal to actually become false inside the
loop. With a local name, yes, I would expect it to at least have a
chance of becoming false.
> Next problem, what the heck is "res"? We're not back in the punch-card
> days. We don't have to abbreviate variable names to save columns.
> Variable names are supposed to describe what they hold, and thus help
> you understand the code. I have no idea what "res" is supposed to be.
> Residue? Result? Rest_of_items? Response? None of these make much
> sense here, so I'm just left befuddled.
I take it as "result", which makes plenty of sense to me. It's the
thing that's about to be yielded. Given that there's not much else you
can say in a meta-function like zip(), I have no problem with that.
Here's a slightly different example:
def mark_last(it):
it = iter(it)
lastres = sentinel = object()
while "more values coming":
res = next(it, sentinel)
if lastres is not sentinel: yield (lastres, res is sentinel)
if res is sentinel: return
lastres = res
Use of "res" for "result" and "lastres" to mean "res as of the
previous iteration of the loop" seems fine to me.
ChrisA
More information about the Python-list
mailing list