Thinking Pythonically (was Re: gah! I hate the new string syntax)

Kirby Urner urner at alumni.princeton.edu
Sat Mar 3 01:41:20 EST 2001


Donn Cave <donn at oz.net> wrote:

>Quoth Mark Pilgrim <f8dy at diveintopython.org>:

>know, I guess it's not for everyone, but for myself, I want to
>challenge your notion of what it is to think Pythonically.
>
>	Donn Cave, donn at oz.net

Personally speaking, I found Pilgrim's defense of list 
comprehensions cogent, and I think his 'Dive Into Python' 
is pretty good.  So maybe he should have used {}.items() 
more often -- I bet he will in future.

I'm another one of those who did NOT come to Python from an 
extensive career in UNIX and related paraphernalia (awk yak 
gak...).  I've got Linux in a partition and visit it once in 
a while -- glad it's there, happy to see that world expanding 
by leaps and bounds.

My first exposure to computing was via the mainframe in college,
an IBM 360/370 VM with APL terminals scattered around campus.
In my psyche, Python is a lot like APL, but with the little
geeky greek-looking symbols replaced by more ordinary-looking
characterstrings.

I've studied Perl to a beginner level, but I think it's somewhat
ugly to look at.  Whereas I'm sure its crypto-compression 
attracts a lot of people, I find Python better because cleaner
somehow (simpler) -- dunno if that's what you were getting at
(your opinion sounds more expert/refined).  But that in no way 
means I think it's a "language war" with Perl -- I hope Perl 
sticks around forever, like Python will.

Pilgrim's use of list comprehensions wherever suitable doesn't
seem a dive into an arcane or cryptic style.  The 'map' thing
was cumbersome, because if your function had two or more 
parameters, but you wanted to hold one or more of them constant, 
you had do to things like:

  >>> def f(a,b): return a+b

  >>> f(1,1)
  2
  >>> g = lambda x: f(1,x)
  >>> map(g,[1,2,3,4])  # or collapse this + above into one line
  [2, 3, 4, 5]

or

  map(f,[1]*4,[1,2,3,4])
  [2, 3, 4, 5]

But with list comprehension, you just go:

  [f(1,x) for x in [1,2,3,4]]
  [2, 3, 4, 5]

That's a lot less "beating around the bush" to get that one
argument to hold at 1, letting the other vary.

Kirby







More information about the Python-list mailing list