Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sure. print sum(map(float, open('somefile.txt')))


That's a bit of cheating, as it removes 'lines' or 'line' from code, decreasing readability. So to be fair you want to add it back: print sum(map(float, open('somefile.txt'))) # over lines or print sum(map(lambda line: float(line), open('somefile.txt')))

Either one is all right, but personally I prefer list comprehension notation.

As per Guido: ... and that a more concise notation for anonymous functions would make map() more attractive. Personally, I disagree—I find the list comprehension notation much easier to read than the functional notation, especially as the complexity of the expression to be mapped increases. ...


In Python it's well known that the default iterator on a file gives you the lines. Arguably a function would've been a clearer design, but this is idiomatic in the language we have.

As far as map vs. list comprehension, yes, I prefer the comprehension in the cases where map would need lambda. Here that's not the case. Per "as the complexity of the expression to be mapped increases", this expression 'float' is the simplest possible.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: