if/elif chain with assignment expressions

Paul Rubin http
Mon Jul 12 16:08:51 EDT 2004


PeterAbel at gmx.net (Peter Abel) writes:
> But always there was a little pitfall cause an assignment
> and a comparison of equality were legal C-code. And the compiler did
> what I wrote and not alway what I wanted it to do. 

I have never for the life of me comprehended why so many people think
that's a problem.  Yes, you can confuse '=' with '==', but you can also
confuse '<' with '<=', or '<' with '>'.  Anyway, the usual solution
is to make the assignment-expression operator something like ':='
(the standard assignment operator in Algol) so that '=' in an expression
remains invalid.  That seems to make the '='/'==' confusion go away.

> Let me change your lines to the followings:
> 
> >    if (assign('y',f(x)) < 5:
> >        fred(y)
> >>> def assign(symbol,value):
> ... 	globals()[symbol]=value
> ... 	return value

Yuch, now you're going to make a GLOBAL variable to hold that saved
value?  You're surely better off using a class instance or closure.
But either way is a mess compared to just using a local variable in
the natural way.



More information about the Python-list mailing list