[Python-Dev] list += string??

David Abrahams dave at boost-consulting.com
Thu Aug 26 16:58:26 CEST 2004


Tim Peters <tim.peters at gmail.com> writes:

> [David Abrahams]
>> I just discovered the following behavior:
>>
>>  C:\Documents and Settings\dave>python
>>  Python 2.3 (#46, Aug 25 2003, 18:37:29) [MSC v.1200 32 bit (Intel)] on win32
>>  Type "help", "copyright", "credits" or "license" for more information.
>>  >>> prefix = ['foo', 'bar', 'baz']
>>  >>> prefix += 'bomb'  # meant to write prefix += [ 'bomb' ]
>>  >>> prefix
>>  ['foo', 'bar', 'baz', 'b', 'o', 'm', 'b']
>>  >>>
>>
>> Is it new and/or intentional?  I would have expected an error from the
>> +=.  I was very surprised when my error "passed silently".
>
> It was new when the iterator protocol was introduced.  It wasn't
> explicitly intended that you get surprises for strings specifically,
> but it was explicitly intended that
>
>     list += whatever
>
> work like
>
>     list.extend(whatever)
>
> and that whatever can be any iterable object.


I figured as much.

> Since strings are iterable objects, it's no more or less surprising
> than that 'for ch in "bomb"' works too <wink>.

I want my ,= operator!

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com



More information about the Python-Dev mailing list