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

Mark Pilgrim f8dy at yahoo.com
Fri Mar 2 15:13:20 EST 2001


"Donn Cave" <donn at u.washington.edu> wrote in message
news:97opsb$nhu$1 at nntp6.u.washington.edu...
> Quoth "Mark Pilgrim" <f8dy at yahoo.com>:
> | 1. I used Python 1.5.x very little; I really only ramped up when 1.6/2.0
was
> | released.  I *like* list comprehensions.  I prefer them over map/lambda
> | because of scope issues, and I prefer them over for loops because they
force
> | me to think Pythonically, and they emphasize Python's strengths (less
code,
> | richer data structures).  To my eyes, list comprehensions fulfill
Python's
> | promise of being readable pseudo-code, not readable line noise.  YMMV.
>
> Pseudo-code is the language you don't have to know, to understand,
> right?  Familiar symbols, unambiguous syntax.  Could you articulate
> your thoughts on this, how do list comprehensions improve on their
> alternatives, there?

Well, list comprehensions are unambiguous in the sense that they are a
specific syntax -- they map a list into another list, and (possibly) filter
out some elements along the way.  Using a for loop and an if statement to do
this is ambiguous in the sense that you could use that physical code
structure for anything, not just for accomplishing the logical goal of
mapping a list.  Glancing at a for loop, you couldn't just say "this bit of
code maps a list into another list" without examining the code thoroughly
and making sure that's really what it was doing, that there weren't any side
effects along the way, etc.  Glancing at a list comprehension, you instantly
know what it is, because the syntax is specific instead of general.  (Note
this does not mean you know what it *does*, just that you can reasonably
rule out lots of things that it doesn't do.)

> What does "think Pythonically" mean to you?  What are you thinking of
> when list comprehensions make you cite "less code, richer data structures"
> as a strength of Python?  Can you talk us crotchety old-timers through
this?

For example, I recently had to write a Python script that locked ClearCase
VOBs (it is not required to understand what those are to follow this
example).  You can do this on the command line: ("cleartool lock vob1 vob2
vob3..."), but the only way to get the list of VOBs is with another command
line tool ("cleartool lsvob -l"), which returns the VOB name on one line
("Tag: <vob>") plus lots of information we don't care about, like this:

Tag: /vobs/intranet
  Global path: ...
  Server host: ...
Vob on host: ...
...

Tag: /vobs/project
  Global path: ...
  Server host: ...
Vob on host: ...
...

If you think of this output as one long string, you can write a for loop
that searches for each "Tag:" and parses out the rest of the line to get a
single VOB name.  But don't do that, that's Visual Basic thinking!
(Actually in my case it's Powerbuilder thinking, but it's just as bad.)
Instead, I force myself to think of the output as a list of strings, where
each line is an element.  This is very easy using readlines():

>>> output = os.popen("cleartool lsvob -l").readlines()

Now I think: OK, I have a list of lines, but I only want some of them -- the
ones starting with "Tag:" -- and I want to throw the rest away.  I could do
this with a for loop, but that's (a) more typing, and (b) more ambiguous
(see above).  Since I have the lines in a list, I can use a list
comprehension:

>>> [line for line in output if line[:4]=='Tag:']
['Tag: /vobs/intranet\\012', 'Tag: /vobs/project\\012']

Now I think: OK, I have a list of the lines I want, but I just want the
name, not the "Tag:" prefix or all the whitespace.

>>> vobs = [line.replace('Tag:', '').strip() for line in output if
line[:4]=='Tag:']
>>> vobs
['/vobs/intranet', '/vobs/project']

Now I think: great, I have a list of VOBs, now I need them space-separated
and integrated into the rest of my command line:

>>> "cleartool lock %s" % " ".join(vobs)
'cleartool lock /vobs/intranet /vobs/project'

And that's what I call thinking Pythonically.  YMMV.

-M
You're smart; why haven't you learned Python yet?
http://diveintopython.org/






More information about the Python-list mailing list