(original) (raw)

That must be so that an immutable type can still implement \_\_iop\_\_ as an optimization.

On Mon, Nov 3, 2014 at 9:10 AM, Antoine Pitrou <solipsis@pitrou.net> wrote:
On Mon, 3 Nov 2014 09:05:43 -0800
Guido van Rossum <guido@python.org> wrote:
> Sorry, was too quick. For immutable types \_\_iop\_\_ may not exist and then
\> the fallback machinery should work normally using NotImplemented. But if
\> \_\_iop\_\_ exists it can choose not to allow \_\_rop\_\_, because the type would
\> presumably change. This is probably more predictable. I don't even know if
\> the byte code interpreter looks for Not implemented from \_\_iop\_\_.

Apparently it can tell it to fallback on \_\_op\_\_:

\>>> class C(list):
... def \_\_iadd\_\_(self, other):
... print("here")
... return NotImplemented
...
\>>> c = C()
\>>> c += \[1\]
here
\>>> c
\[1\]
\>>> type(c)



Regards

Antoine.
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/guido%40python.org



--
--Guido van Rossum (python.org/\~guido)