inconsistency with += between different types ?
Andreas.Leitgeb at siemens.at
Fri Aug 9 17:35:14 CEST 2002
Huaiyu Zhu <huaiyu at gauss.almadan.ibm.com> wrote:
> Implementation. Even though (logically) __iadd__ does not make sense for
> immutable types, the proposal does not forbid it.
Actually it indirectly does.
If an immutable class defined __iadd__ as a copy of __add__ (for
performance reason, as was pointed out), then after the proposal,
using += on an instance of that class will end up having no effect
at all :-(
I consider this copying a bad thing anyway, and "performance" a poor
excuse for it, but I'm definitely not the one asked to judge it.
> There are other ways to solve this problem. One is splitting the
> two semantics into two symbols like += and +!, for example.
With such a semantic I would not even have started this thread, (as the
compiler would have reminded me that += on tuples,ints and strings would
have been illegal).
Anyway, splitting up these semantics at this time would surely invalidate
much more existing code, than ignoring the returnvalue of __iadd__.
> Another is to allow a definition like __iadd__=None to indicate the
> rebinding semantics, for example.
Assigning None to __iadd__ currently "invalidates" += altogether for
that class, leading to an Exception for calling something uncallable.
I don't know, whether existing classes make use of that behaviour.
If detecting a None is/were considerably faster than detecting NotFounds, it
may be worth adding to the proposal.
Newsflash: Sproingy made it to the ground !
read more ... <http://avl.enemy.org/sproingy/>
More information about the Python-list