On Thu, Apr 19, 2012 at 1:21 PM, Alek Storm alek.st...@gmail.com wrote:
On Thu, Apr 19, 2012 at 7:12 AM, lkcl luke luke.leigh...@gmail.com wrote:
On Thu, Apr 19, 2012 at 12:20 PM, Alek Storm alek.st...@gmail.com wrote:
Why not use list comprehension syntax?
because it's less characters to type, and thus less characters to
read. i find that syntax incredibly klunky. left to right you're
reading, you declare something to be the case... and then oh whoops
actually it's not really the case because it's modified by a list
thing - it breaks reading expectations.
that's what i meant about beauty and elegance. the bang per buck
ratio in python, results obtained for the number of characters used,
is higher, and that's something that i personally find to be a
priority over speed.
Did I miss something? `[a+1 for a in some_list]` is shorter than both
`map(lambda a: a+1, some_list)` and `some_list.map(lambda a: a+1)`.
:)
yes you missed something. :)
a) if you use that lambda a:a+1 a lot, you make it an actual
function, don't you? even for clarity you'd still probably use a
function not a lambda. i use map quite a lot, filter and reduce not
so much. a more real-world example was one that i actually gave
initially: map(str, l). or, the syntax i really prefer which doesn't
exist: l.map(str). or, one that i also use in web applications or
their back-ends: map(int, l). if you have a comma-separated set of
variables in a single string, like this: 5, 20, 3, i tend to use
this:
from string import strip
l = map(int, map(strip, l.split(,)))
ok to make it clearer i sometimes do that on 3 separate lines.
i'd _love_ it to be this:
l = l.split(,).map(strip).map(int)
or even better would be this:
l = l.split(,).map(strip, int)
b) read the text from left to right, in plain english:
* map(str, l): you're going to map i.e. apply a string function to a
list's members.
(now you see why i keep getting confused with map, because the
natural language version of this is map a list's members to a string
- the other way round)
* a+1 for a in l: take an expression which is a mathematical
operation and therefore has the expectation that its arguments are
mathematical in nature. then oh damn it wait a minute, actually
there's more going on here: for every variable in a list, use the
variables in the expression to make a new list...
i'm belabouring the point (not entirely intentionally) but you see how
clumsy that is? it's probably just as complex in the actual
lexer/grammar file in the http://python.org source code itself, as it
is to think about in real life and to psychologically parse in
english. you actually have to think *backwards*!
is that clearer, or have i added mud? :)
l.
--
http://mail.python.org/mailman/listinfo/python-list