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

Gustavo Cordova gcordova at hebmex.com
Wed Feb 20 19:31:02 EST 2002


> 
> =/== 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
> 

It's not such a weak reason: it happens, a lot. And not only
among newbies, it's only a small typo, but it's hideous
in it's consequences. I like not being able to use "="
in an expression.

Now *THIS*:

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

Indeed, this is very readable. The ":=" asignment operator
is used in Pascal, if I remember correctly; another language
where you can't assign inside an expression.

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

But it only feels contorted, to you. To me it feels quite
natural:

	1. Set up the loop conditions and variables.
	2. Do the loop.
	3. Exit the loop.

Yes, your ":=" suggestion is valid, and it removes the "=/=="
readability / typo reason. But if it's not there, and if
the people maintaining your code are going to get all huffy
because it's "spelled out" instead of being in a compact
form, then the problem is another, and not the language.

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

Ahh, but augmented assignments are not assignments
inside expressions, but expressions inside assignments.

:-)

Then again, maybe all the newbies used other languages
which feature the assignment / expression duality. Also,
this "limitation" (of not allowing assignments in expressions)
is clearly expressed in the docs, which means that newbies
nowadays are simply NOT reading the documentation before
launching into new waters. That severely downgrades the
quality of the questions they might pose, no?

Just count how many times the "static vs dynamic typing"
thread has reappeared, it just won't die. And it's always
*another* newbie which comes in and asks the same question,
instead of learning, slowly, breathing calmly, how to best 
use this shiny new tool.

BUT, they keep coming to Python, why?

Is it because it lacks something?

Or is it because it has something beyond those other tools?

I'm sure that it'll keep evolving, it needs to. But there's
something inherently *nice* which comes into play when
the code's all spelled out, doing the clearest thing
possible, which just isn't there in Lisp, C, C++, etc.

Anyway, it's just my opinion. :-)

-gustavo




More information about the Python-list mailing list