Re:Zed finishes Learn Python The Hard Way
by RyanMcGreal on Wednesday September 29, 2010 @12:52PM
(#1740081)
You can also combine maps and filters in a list comprehension:
[ Parent
| on HN ]
Re:Zed finishes Learn Python The Hard Way
by zedshaw on Wednesday September 29, 2010 @01:54PM
(#1740348)
Read that out loud and tell me that doesn't grate on your ears? That's my basic test if code for the book would be understood by the reader. What you have here, this "symbol soup" is what makes programming hard for people in the beginning.
Re:Zed finishes Learn Python The Hard Way
by angusgr on Wednesday September 29, 2010 @07:48PM
(#1741639)
Some of the simpler list comprehensions (like the student one two parents up) are fairly "symbol soup" free, though - and a very clean way of expressing what you want.
OTOH, I can see how they could still be a hard concept for a beginner who hasn't done any functional programming before, particularly because they don't read front-to-back like most imperative statements.
EDIT: You can always split complex ones up as well, for most common usages:
meeps = [ s for s in str_list if s[:4] == 'meep' ]
suffix_str_lst = ['{0}_suffix'.format(m) for m in meeps ]
This one was probably a bit simple to really need that, but it's a bit easier to read. :).
Re:Zed finishes Learn Python The Hard Way
by RyanMcGreal on Wednesday September 29, 2010 @02:19PM
(#1740471)
I actually find it really elegant. I fell in love with comprehensions as soon as I discovered them. They were the terse, expressive syntax I'd long been searching for.