Behavior of += (was Re: [Python-Dev] Customization docs)

Emile van Sebille emile at fenx.com
Sat Jun 1 16:53:28 CEST 2002


<jepler at unpythonic.net> wrote:
> IMO the following is an odder behavior:
> >>> t
> ([1], [2], [3])
> >>> t[1] += [3]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: object doesn't support item assignment
> >>> t
> ([1], [2, 3], [3])
> 

Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
>>> class myTuple(tuple):
...     def __setitem__(self, attr, value):
...             print 'in setitem:', attr
...
>>> a = myTuple(([1],[2],[3]))
>>> a
([1], [2], [3])
>>> a[1] += [4]
in setitem: 1
>>> a
([1], [2, 4], [3])
>>>


Here's another that's differently wrong:
>>> a = ('aaa',)
>>> a[0]+='bbb'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> a
('aaa',)
>>>




-- 

Emile van Sebille
emile at fenx.com

--------- 




More information about the Python-list mailing list