Relax Syntax for Augmented Arithmetic?

Chris Rebert clp2 at
Sun Jan 18 12:31:46 CET 2009

On Sun, Jan 18, 2009 at 2:56 AM, andrew cooke <andrew at> wrote:
> Context -
> model#object.__iadd__
> Just a suggestion I thought I'd throw out...  There's a restriction in
> the language implementation on exactly what can go the left of an
> augmented arithmetic expression.
> For example:
>>>> a = 3
>>>> a **= 2
> is ok, but:
>>>> class Foo():
> ...   def __init__():
> ...     self.a = 3
> ...   def __ipow__(self, x):
> ...     self.a **= x
> ...
>>>> Foo() **= 2
>  File "<stdin>", line 1
> SyntaxError: illegal expression for augmented assignment
> Now unless I've done something stupid above (always a possibility :o)
> the implementation seems a bit strict (is it really a *syntax* error?
> - I am not sure exactly what the restriction is).

IIRC, you can only assign to:
- variables (x = ...)
- attributes (x.y = ...)
- elements (x[y] = ...)

Anything else doesn't make sense to assign to because it's not a
"storage box" so to speak. There's no way to work out what is meant.
In your case, you're assigning to a *value*, specifically a new
instance of the Foo class, which is nonsensical; instead of a "box",
you're trying to assign to a "value", something that gets stored in
boxes. By comparison, '2 = 5' and '[1,2] = 7' would seem to have some
sort of meaning under your system, which IMHO seems preposterous.
Now true, you are using augmented assignment, which in certain cases
is translated to a method call, but in principle the augmented
assignment (e.g. x += y) should have roughly the same effect as the
non-augmented equivalent (x = x + y), and the fact that a method call
is involved is merely an implementation detail of sorts.
Therefore, Python requires you to rewrite the code in some other way
that makes your intentions more clear. For instance, why not use the
<< operator instead?


Follow the path of the Iguana...

More information about the Python-list mailing list