On Sat, Feb 14, 2015 at 11:12 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
> The current design of Python guarantees that an object always gets a setattr or setitem when one of its elements is assigned to. That's an important property, for the reasons I suggested above. So any change would have to preserve that property. And skipping assignment when __iadd__ returns self would not preserve that property. So it's not just backward-incompatible, it's bad.

--> some_var = ([1], 'abc')
--> tmp = some_var[0]
--> tmp += [2, 3]
--> some_var
([1, 2, 3], 'abc')

In that example, 'some_var' is modified without its __setitem__ ever being called.

not really -- an object in the some_var is modified -- there could be any number of other references to that object -- so this is very much how python works.

The fact that you can't directly use augmented assignment on an object contained in an immutable is not a bug, but it certainly is a wart -- particuarly since it will raise an Exception AFTER it has, in fact, performed the operation requested.

I have argued that this never would have come up if augmented assignment were only used for in-place operations, but then we couldn't used it on integers, which was apparently desperately wanted ;-) (and the name "augmented assignment" would be a good name, either...).

I don't know enough about how this all works under the hood to know if it could be made to work, but it seems the intention is clear here:

object[index] += something.


is a shorthand for:

tmp = object[index]
tmp += something

or in a specific case:

In [66]: t = ([], None)

In [67]: t[0].extend([3,4])

In [68]: t
Out[68]: ([3, 4], None)


Do others agree that this, in fact, has an unambiguous intent? And that it would be nice if it worked? OR am I missing something?

-Chris




--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov