Why is Python popular, while Lisp and Scheme aren't?
Brian Quinlan
brian at sweetapp.com
Tue Nov 12 22:44:44 EST 2002
> 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. :-(
People make frequent mistakes with inline assignment in other languages
(e.g. C), so it shouldn't be allowed. I don't think that indentation has
anything to do with this.
> 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)):
> ...
> ...
I'd write that as:
def process_type_1(first, second, ...):
process type 1 record ...
def process_type_2(first, second, ...):
process type 2 record ...
processing_rules = [(r'....', process_type_1),
(r'....', process_type_2),
...]
compiled_rules = [(exp.compile(), func) for (exp, func) in
processing_rules]
for l in file.readlines():
for exp, func in compiled_rules:
match = exp.match(l)
if match is not None:
apply(func, m.groups())
break
Notice that:
- it is very easy to add new record types
- record processing is more modular
- the code is pretty simple
Cheers,
Brian
More information about the Python-list
mailing list