Need better string methods

Donn Cave donn at drizzle.com
Mon Mar 8 05:42:42 CET 2004


Quoth David MacQuigg <dmq at gain.com>:
...
| # Current best Python:
| clean = [' '.join(t.split()).strip('.') for t in line.split('|')]
|
| This is too much to expect of a non-programmer, even one who
| undestands the methods.  The usability problems are 1) the three
| variations in syntax ( methods, a list comprehension, and what *looks
| like* a join function prefixed by some odd punctuation), and 2) The
| order in which each step is entered at the keyboard.  ( I can show
| this in step-by-step detail if anyone doesn't understand what I mean.)
| 3) Proper placement of parens can be confusing.

I quite agree, except of course like most other followups I don't
necessarily agree that this is current best Python.

Skip's probably right that a procedural style is best for casual
Python users, and maybe for anyone.  It really clears up order of
evaluation, for one thing.

Just for variety, here's how I imagine a Haskell user would
write it (omitting the definition of strip, join and split):

clean line =
	map ((strip '.') . (join ' ') . (split ' ')) (split '|' line)

Still sacrifices readability for the sake of doing it all in one
expression, and is overly abstract as usual, but at least the syntax
is relatively simple.  It avoids the right/left association switching
that you got with your "best" version above - queer thing about classic
OOP languages like Python, that functions associate left with their
primary object, but right with the rest of their parameters, and in
expressions like that it makes me feel a little sea-sick.

Possibly unfamiliar notations above are pretty simple -
   Function composition:  (f . g . h) a = (f (g (h a)))

   Partial application:  (split ' ') b = split ' ' b
   (which makes for a different natural parameter order than Python's)

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list