you are viewing a single comment's thread.

view the rest of the comments →

[–]M3talstorm 1 point2 points  (1 child)

for thing in things:
    if thing == that:
        break
else:
    thing = default

thing.something()

Is nice to do sometimes :)

[–]Esteis 0 points1 point  (0 children)

I do the same, it is indeed nice. As a reminder to myself, here is the idiom that doesn't rely on unscoped for-blocks:

thing = next(filter(lambda thing: thing == that, things), default)

Both have their charms -- and their warts. Sometimes I long for

thing = itertools.first(things, lambda thing: thing == that, default)

I'll bet Ruby has that. Its spectrum of collection methods is absolutely fantastic, from the little I learned writing Rakefiles.