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

Dan Schmidt dfan at dfan.org
Tue Nov 12 23:41:25 EST 2002


Martin Maney <maney at pobox.com> writes:

| 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.

Yeah, this is the main thing that annoys me about not being able to
use assignments as expression.

| 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):
|       ...
|     ...

I've done this too for simple scripts.  If you're writing reusable
code, one thing you do have to worry about with this approach is
thread safety.

Another alternative, which is often possible because this stuff tends
to happen inside line-processing loops, is:

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

Dan

-- 
http://www.dfan.org



More information about the Python-list mailing list