Why x+=1 doesn't return x value instead of an object

Chris Angelico rosuav at gmail.com
Sat Oct 31 00:00:37 EDT 2020


On Sat, Oct 31, 2020 at 2:41 PM Jon Ribbens via Python-list
<python-list at python.org> wrote:
>
> On 2020-10-31, Chris Angelico <rosuav at gmail.com> wrote:
> > On Sat, Oct 31, 2020 at 1:51 PM Jon Ribbens via Python-list
> ><python-list at python.org> wrote:
> >>
> >> On 2020-10-31, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> >> > Siddhharth Choudhary <siddharth654choudhary at gmail.com> writes:
> >> >>I want to know why x+=1 does not return the value of the variable.
> >> >
> >> >   Which value? The old or the new one?
> >> >
> >> >   Expressions never return values.
> >>
> >> Except when they're assignment expressions, when they do.
> >
> > Expressions DO have values, but assignment is a statement, not an
> > expression. (Assignment expressions don't have augmented forms, so
> > there's no ambiguity.)
>
> Sorry, could you say that again in English?

An expression has a value. For instance, "x + 1" is an expression; its
value is whatever you get by adding one to x. The expression "lambda
x, y: x + y" also has a value - specifically, a function. Every
expression has a value (unless it raises an exception at runtime).

Statements do not have values. There's no meaningful value in "if x >
2:" or "pass". They do their stuff and that's that.

(Some languages have expressions that can contain statements, such as
function definitions. That doesn't change this, since the value of the
expression is still the function object.)

In Python, "x = 1" is a statement and does not have a value. So is "x
+= 1", which is called an augmented assignment, and it does both
assignment and addition (possibly in-place). There is the special
assignment expression form "x := 1", which does have a value; however,
there's no augmented form "x +:=1" or "x :+= 1", so there's no way for
the augmented form to have a value.

In languages such as C, where augmented assignment is an expression,
it's theoretically possible to write something like this:

x += y -= z *= 3;

This is not good code. Don't do it. But it IS legal. :) The most
common valid uses of assignment as an expression in C are all with the
simple form, such as quickly setting a number of things to the same
value:

x = y = z = 100;

Messing around with augmented forms as expressions is usually a bad
idea. There's one very very specific mutator (technically two - or
four) in C that is useful as an expression, and that's the in-place
increment/decrement operator; and it's very carefully designed so you
get the choice of whether you want the before or the after. You can
write "array[idx++]" to advance to the next slot, or "id = ++max_id"
to claim the next ID and bump up the top marker. This has been
proposed for Python a few times, but the use cases have never been
strong enough to get it added to the language.

I don't think we'll ever see "x += 1" become an expression in Python.

ChrisA


More information about the Python-list mailing list