3 ms·
`a += b` does something along the lines of `a = a.__iadd__(b)`. Here `__iadd__` performs the actual append, but then the assignment fails.
by bakery2k 7y ago
`a += b` does something along the lines of `a = a.__iadd__(b)`. Here `__iadd__` performs the actual append, but then the assignment fails.
- avip 7y agoDoes it though? So why doesn't a += b change the object id id(a) ?
- bakery2k 7y agoBecause if `a` and `b` are lists, `a.__iadd__(b)` mutates `a` in place, then returns it.
- avip 7y agoRight, got it.
- masklinn 7y agoAs OP noted, given the existence of type(a).__iadd__[0] `a += b` essentially desugars to: `a = a.__iadd__(b)`. list.__iadd__ is (sadly) defined as something like def __iadd__(self, other): self.extend(other) return self So it's possible to have __iadd__ itself succeed modifying the list in place but then the "no-op" reassignment fail. [0] like many data model operations there's really a bunch of fallbacks depending on what is and is not implemented
- avip 7y agoI'd almost consider this a subtle bug? None of the other += operators returns a value.
- masklinn 7y ago> I'd almost consider this a subtle bug? It's not exactly a bug but it is a somewhat unexpected behaviour and IIRC Guido regretted that list.__iadd__ was overridden this way. > None of the other += operators returns a value. It's not the operator which returns a value, it's the data model hook. The data model requires that it return a value: https://docs.python.org/3/reference/datamodel.html?#object.__iadd__ https://docs.python.org/3/reference/datamodel.html?#object._... > These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self). Really the issue is that the "in-place" hooks are simply weird.