[Python-Dev] SyntaxError: can't assign to function call
Terry Reedy
tjreedy at udel.edu
Wed Aug 9 19:41:32 CEST 2006
"Neal Becker" <ndbecker2 at gmail.com> wrote in message
news:ebd2sp$nnv$1 at sea.gmane.org...
> class X (object):
> pass
>
> X() += 2
>
>> SyntaxError: can't assign to function call
>
> Suppose I actually had defined __iadd__ for class X. Python says this
> syntax is invalid. I wish is wasn't.
If you translate to x() = x() +2, with x called just once, it does not make
sense. You can bind to names and slots in compound objects, but not to
objects.
> Here's where I might use it. Suppose I have a container class. Suppose
> I
> could make a slice of this container, like so:
>
> c = Container()
> s = Slice (c, 0, 10)
>
> where "Slice" is a class. Then:
>
> s += 2
>
> Would perform iadd on the slice of the container. A convenient syntax
> would
> be:
>
> Slice (c, 0, 10) += 2
If this were made to work, the unbound object you are making an
'assignment' to, purely for side effect, would disappear. So if you don't
want a separate object to be named and kept around, give your container a
sliceadd method:
c.sliceadd((0,10), 2)
>
> Does anyone else think this would be a good addition to Python?
I don't. Augmented assigment is confusing enough to many.
Terry Jan Reedy
More information about the Python-Dev
mailing list