two ideoms at one blow: line-reading and regexp-matching

Paul Rubin phr-n2002a at nightsong.com
Wed Feb 20 19:24:25 EST 2002


Gustavo Cordova <gcordova at hebmex.com> writes:
> > Next question: why is muddying the statement/expression distinction a
> > detriment?  As opposed to the alternate view, that the 
> > statement/expression
> > distinction is itself a detriment?
> > -- 
> 
> Or, if I may rephrase the question:
> 
> Why is the statement/expression distinction a detriment,
> as opposed to the alternate view, that the statement/expression
> distinction is a virtue?

Because we've already seen, assignments in expressions are useful.
And blurring the statement/expression distinction has proven quite
successful in languages like C and Lisp.

Anyway, language attempts to impose "morality" on programmers tend to
annoy programmers more than they makes code any cleaner.

> I, at least, prefer the legibility and explicitness
> of the current situation, where there can be no
> misunderstanding, versus the common "Oops, I typed '='
> instead of '==' mistakes".

But we just got through agreeing that allowing += inside expressions
doesn't lead to =/== mistakes.

Where this is leading is the proposal from a month or so ago, that :=
(rather than =) be allowed for ordinary assignments in expressions.

=/== confusion is really a weak reason to not allow assignments in
expressions.  Do you have some other reasons?  If not, prohibiting
them is just imposing a subjective opinion of which method makes
code more readable.  I certainly find

   while (m := re.match(pat,x)):  blah()

more readable than some multi-statement contortion for doing the same
thing, or worse, using some kludgy special-purpose class that's
defined far away in the code and maybe done differently from one
program to the next.  It's possible that some other people find the
contorted versions more readable, but they're not the ones who have to
maintain my code.

Maybe there's a principle to be found here: "design by FAQ".  If newbies
keep arriving and asking over and over again "how do I do X?", maybe
there's a good reason for having a way to do X.  We've seen that already
for augmented assignments.



More information about the Python-list mailing list