[Python-Dev] INPLACE_ADD and INPLACE_MULTIPLY oddities in ceval.c
Armin Rigo
arigo at tunes.org
Wed Mar 29 12:45:40 CEST 2006
Hi Greg,
On Wed, Mar 29, 2006 at 12:38:55PM +1200, Greg Ewing wrote:
> I'm really thinking more about the non-inplace operators.
> If nb_add and sq_concat are collapsed into a single slot,
> it seems to me that if you do
>
> a = [1, 2, 3]
> b = array([4, 5, 6])
> c = a + b
>
> then a will be asked "Please add yourself to b", and a
> will say "Okay, I know how to do that!" and promptly
> concatenate itself with b.
No: there is a difference between + and += for lists. You can only
concatenate exactly a list to a list. Indeed:
>>> [].__add__((2, 3))
TypeError: can only concatenate list (not "tuple") to list
By contrast, list += is like extend() and accepts any iterable.
So if we provide a complete fix, [].__add__(x) will be modified to
return NotImplemented instead of raising TypeError if x is not a list,
and then [1,2,3]+array([4,5,6]) will fall back to array.__radd__() as
before.
I'll try harder to see if there is a reasonable example whose behavior
would change...
A bientot,
Armin
More information about the Python-Dev
mailing list