gah! I hate the new string syntax

Mark Pilgrim f8dy at yahoo.com
Fri Mar 2 11:26:48 EST 2001


"Fredrik Lundh" <fredrik at pythonware.com> wrote in message
news:B7In6.16281$Qb7.2658902 at newsb.telia.net...
> Sean 'Shaleh' Perry wrote:
> > return ";".join(["%s=%s" % (k, params[k]) for k in params.keys()])
> >
> > every day python seems to be moving closer to the line noise aspect of
> > coding.
>
> you're using list comprehensions and the wrong dictionary method,
> and then you're complaining about string methods? ;-)
>
> ("items()" returns a key/value tuple that you can pass directly to
> the formatting operator.  and nobody will complain if you stick to
> string.join for stuff like this...)

I recognize this code as an example from my book "Dive Into Python",
specifically http://diveintopython.org/odbchelper_join.html, and would like
to (partially) defend it in that context.

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.
2. I considered using .items() and rejected it for this example, because I
didn't want to get into explaining multi-variable assignment in chapter 1.
(It is discussed later in chapter 3:
http://diveintopython.org/fileinfo_multiassign.html, including an example of
iterating through a dictionary using items().)  I still believe this was the
right choice; you can't cover everything at once, even when you're talking
to smart people.  However, I will add a footnote after the discussion of
this example, noting that there is an even *more* elegant way of
accomplishing the same thing using multi-variable assignment, to be covered
in chapter 3.
3. I *like* string methods, even .join().  I am aware that this puts me in a
very small minority on comp.lang.python.  But I would maintain that
";".join(...) only seems odd to you because you see it as the result of a
strained migration from string.join(..., ";"), instead of seeing it with
fresh eyes.  However, I may add a footnote explaining the old style
string.join, if only because new programmers will eventually come across old
code that uses it.

If current trends continue, *far more people* will learn Python 2.0 than
ever learned Python 1.x.  They will all take list comprehensions and string
methods for granted, and stare at you blankly when you reminisce about the
good old days of Python 1.5.2.

-M feeling-old-and-crotchety-already-ly
You're smart; why haven't you learned Python yet?
http://diveintopython.org/






More information about the Python-list mailing list