[issue45293] List inplace addition different from normal addition

2021-09-26 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

Kapil, this behavior was intentional (not a bug), but later regarded as a 
design mistake.  GvR has said that if he had to do it over again list.__iadd__ 
would only accept other lists.  The problem arises when people write "somelist 
+= 'hello'" and expect it to add a single string with one word rather than five 
one letter strings.  That said, the current behavior is guaranteed, people rely 
on it, and we cannot change it.

As Karthikeyan points out, this is documented.  However since the docs have 
grown so voluminous, it is often difficult to find any one particular fact.

--
nosy: +rhettinger
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45293] List inplace addition different from normal addition

2021-09-26 Thread Karthikeyan Singaravelan

Karthikeyan Singaravelan  added the comment:

https://docs.python.org/3/faq/programming.html#faq-augmented-assignment-tuple-error

> for lists, __iadd__ is equivalent to calling extend on the list and returning 
> the list. That’s why we say that for lists, += is a “shorthand” for 
> list.extend

This example is calling extend on list of strings with another string as 
argument. Hence the target string is iterated and each character is added as an 
element. __add__ is different compared __iadd__. For += __iadd__ is called if 
defined and falls back to __add__

--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45293] List inplace addition different from normal addition

2021-09-26 Thread Eric V. Smith


Eric V. Smith  added the comment:

For those not in front of a computer, the error is:

>>> l = l + 'de'
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can only concatenate list (not "str") to list

--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45293] List inplace addition different from normal addition

2021-09-26 Thread Kapil Bansal


New submission from Kapil Bansal :

Hi,

I tried addition and in-place addition on a list.

>>> l = ['a', 'b', 'c']

>>> l = l + 'de' (raises an error)

>>> l += 'de'
>>> print(l)
['a', 'b' , 'c', 'd', 'e']

I want to ask why the behaviour of both these are different?? If it is done 
intentionally, then it should be there in the documentation but I am not able 
to find any reference.

--
messages: 402658
nosy: KapilBansal320
priority: normal
severity: normal
status: open
title: List inplace addition different from normal addition
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com