On Wed, Nov 16, 2016 at 07:36:04PM +0000, Paul Moore wrote:
> On 16 November 2016 at 17:50, Ryan Gonzalez <[email protected]> wrote:
> > If someone decided to be weird and have __add__ and __iadd__ do two
> > different things, this would completely break that. Granted, that's a stupid
> > idea to begin with, but it's still poor justification for the code breakage.
>
> If you think of a = a + 1 as "assigning the value of the expression a
> + 1 to a" and a += 1 as "incrementing a by 1" it's neither weird nor
> stupid.
Indeed.
Suppose Python had both mutable and immutable ints (as we have mutable
lists and immutable tuples, which are kinda-lists). Then given
assignment of immutable ints the two forms will be the same. (This is
the status quo.)
a = b = 99
a = a + 1
assert a == 100 and b == 99
and likewise for a += 1. But for *mutable* ints, they may not be. We
might expect:
a = b = mutable(99)
a = a + 1
assert a == 100 and b == 99
no different from the status quo, but:
a = b = mutable(99)
a += 1 # modify a in-place
assert a == b == 100
as is the status quo for lists.
Of course, if we're designing mutable ints, we're free to pick whatever
behaviour we want for both __add__ and __iadd__. But we needn't feel
that they are *necessarily* identical.
--
Steve
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/