[Python-Dev] Re: [PEP] += on return of function call result

Terry Reedy tjreedy@udel.edu
Sat, 17 May 2003 13:34:05 -0400


"Luke Kenneth Casson Leighton" <lkcl@samba-tng.org> wrote in message >
1) what is the technical, syntactical or language-specific reason why
>     I can't write an expression like  f(x) += y ?

In general, ignoring repetition of side-effects, this translates to
f(x) = f(x) + y.
Abstractly, the assignment pattern is target(s) = object(s), whereas
above is object = object.  As some of have tried to point out to the
cbv (call-by-value) proponents on a clp thread, targets are not
objects. so object = object is not proper Python.  The reason inplace
op syntax is possible is that syntax that defines a target on the left
instead denotes an object when on the right (of '='), so that syntax
to the left of op= does double duty.  As Jeff Eppler pointed out in
his response, the compiler uses that syntax to determine the type of
target and thence the appropriate store instruction.  But function
call syntax only denotes an object and does not define a target and
hence cannot do double duty.

The exception to all this is listob += seq, which translates to
listob.extend(seq).  So if f returns a list, f(x) += y could be
executed, but only with runtime selection of the apropriate byte code.
However, if you know that f is going to return a list, so that f(x)+=y
seem sensible, you can directly write f(x).extend(y) directly (or
f(x).append(y) if that is what you actually want).  However, since
this does not bind the result to anything, even this is pointless
unless all f does is to select from lists that you can already access
otherwise.  (Example: f(lista,listb,bool_exp).extend(y).)

Terry J. Reedy