assignment expression peeve

Terry Reedy tjreedy at udel.edu
Sat Oct 18 15:13:26 EDT 2003


To me, simple var=val assignments often correspond to idiomatic
Emglish sentences.
"If your answer to my question is correct, your reward will be three
times the value of that answer" translates to something like
if (answer=response(question)) == question.correct_answer: reward =
3*answer

However, unpacking assignment (which C lacks) strikes me as more
problematical.

>>> def f(): yield 1; yield 2
...
>>> a,b = f()

If this assignment statement were to be an expression, what should be
its value?  The exhausted generator-iterator produced by f()?  That is
gone, I believe, before the actual assignment.  The tuple (a,b)?  That
never exists in the current implementation of the statement.  Or do we
arbitrarily pick the first or last item assigned.

In Python, even single assignment can be problematical when targets
(lvalues in C) mutate (which, again, I believe impossible in C).
Consider
obj.val = 3
where obj.__set/get/attr__ are such that obj.val returns 7.  If we
were to make that statement an expression, should it return the value
given to obj (3) or the value that obj.val results as (7), and which
will be used in following statements?

Terry J. Reedy






More information about the Python-list mailing list