Why is Python popular, while Lisp and Scheme aren't?

Martin Maney maney at pobox.com
Tue Nov 12 19:13:35 EST 2002


Alex Martelli <aleax at aleax.it> wrote:
> But I guess the discipline of having to explain this over and over
> is good for me, just as the hurdle of having to GET it is good for
> the newbies.

I'd love to "get" why some form of assignment on the fly is so evil that we
have to invent hacks to work around its lack over and over again (recipie
1.9 in the Cookbook).  The lack interacts especially badly with a language
where indentation is a mandantory part of the control structure.  :-(

Sure, you *can* work around it, but the best generic solution is ugly
(recipie 1.9), and nicer solutions are an awful waste of programming time nd
effort, and are likely to introduce subtle potential problems, IME.

For example, the reason I'm interested enough to spend time typing this was
a piece of work that included parsing some existing data files.  They were
all flat text, and easily recognized (and dissected) by regexps, but many of
them contained several different record formats (one record == one line,
though).  In some cases there were optional tail fields; in one case there
were variants because the data had accumulated over a long period of time,
and the people entering it had interpreted the instructions differently. 

So the typical processing loop is conceptually simple in a language very
similar to Python:

  for l in file.readlines():
    if (m = re1.match(l)):
      (first, second, ...) = m.groups()
      process type 1 record ...
    elif (m = re2.match(l)):
      ...
    ...

Now, I don't use indents as small as that in real code, and the processing
itself typically added a few levels.  So the straightforward translation of
the simple concept into Python would go marching steadily off towards the
right side of the screen.  Very annoying, and so unnecessary.

I guess it was a good thing I had picked up _Python Cookbook_ shortly before
this mess developed, because it pointed me towards a usable, though klugey,
way out.  As I've said, I think the generic mechanism in the recipie is
rather ugly - not that I can see any better way to solve the problem in
general.  But in this case there was a beautiful, only mildly hazardous
solution.

I wrapped the compiled regexps in a class that saved the results of applying
the regex to a string, of course.

Notationally, this is very nice.  The code now looks like

  for l in file.readlines():
    if re1.match(l):
      (first,second ...) = re1.groups()		# typically
      process type 1 record ...
    elif re2.match(l):
      ...
    ...

The ugly side of this approach is pretty well hidden here, since the pattern
of use makes it extremely unlikely that I'd ever accidentally try to use
that cached state when it was stale, or had been altered.  But I do worry a
little about leaving this to crop up years later... or I would if I weren't
confident that after this transitional phase is complete this interim
conversion code will be very, very dead.  <whistles unconvincingly>

Well, there you go.  I thought I was just going to do some ad-hoc testing of
the new news server setup, and here I am launching my first rant to c.l.py

It's a funny world.




More information about the Python-list mailing list