[Python-ideas] Adding "+" and "+=" operators to dict

Andrew Barnert abarnert at yahoo.com
Sat Feb 14 03:57:43 CET 2015


On Feb 13, 2015, at 18:46, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

> Andrew Barnert wrote:
> 
>> I think it's reasonable for a target to be able to assume that it will get a
>> setattr or setitem when one of its subobjects is assigned to. You might need
>> to throw out cached computed properties, ...
> 
> That's what I was thinking. But I'm not sure it would be
> a good design,

Now I'm confused.

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.

So, I don't know what the "it" you're suggesting as an alternative is. Not calling setitem/setattr, but instead calling some other method? If that method is just a post-change item_was_modified call I don't think it's sufficient, while if it's a pre-change item_will_be_modified(name, newval) that lets you change or reject the mutation it's equivalent to the existing setitem.

> since it would *only* catch mutations made
> through in-place operators. You would need some other
> mechanism for detecting mutations of sub-objects made in
> other ways, and whatever mechanism you used for that would
> probably catch in-place operations as well.
> 
> -- 
> Greg
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list