Re: Framework for a beginner

2012-04-19 Thread lkcl luke
On Thu, Apr 19, 2012 at 12:20 PM, Alek Storm alek.st...@gmail.com wrote:
 On Wed, Apr 18, 2012 at 11:21 PM, lkcl luke.leigh...@gmail.com wrote:

 On Apr 11, 9:11 pm, biofob...@gmail.com wrote:

  I am new to python and only have read the Byte of Python ebook, but want
  to move to the web. I am tired of being a CMS tweaker and after I tried
  python, ruby and php, the python language makes more sense (if that makes
  any sense for the real programmers).

  yeah, it does :)  python is... the best word i can describe it is:
 it's beautiful.  it has an elegance of expression that is only marred
 by the rather silly mistake of not taking map, filter and reduce into
 the list object itself: l.map(str) for example would be intuitive,
 compact and elegant.  instead, i have to look up how to use map each
 and every damn time!  the reason for the mistake is historical: map,
 filter and reduce were contributed by a lisp programmer.  that lisp
 programmer, presumably, was used to everything being function(args...)
 and it simply didn't occur to anyone to properly integrate map, filter
 and reduce properly into the list objects that they work with.


 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.

 you don't *have* to use lambdas with map and reduce, you just have to
use a function, where a lambda happens to be a nameless function.

 another example of the compactness of python is kicking around
somewhere, i wish i could remember where it is.  it compares scheme
with python and java.  scheme does this amazing programming thing in
a single operator, expressed in 3 characters.  python manages the same
thing in about 10, and java requires *six* lines!


 It gets you map and filter
 functionality for free, and is more readable than python's clunky version of
 lambdas. I believe they're faster than the for-loop equivalents, but I'm not
 sure about the actual map() and filter() functions (reduce() was removed
 from 3.0 for reasons I will never understand).

 likewise.

 /salutes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Framework for a beginner

2012-04-19 Thread lkcl luke
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