Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List inplace addition different from normal addition #89456

Closed
devkapilbansal mannequin opened this issue Sep 26, 2021 · 4 comments
Closed

List inplace addition different from normal addition #89456

devkapilbansal mannequin opened this issue Sep 26, 2021 · 4 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@devkapilbansal
Copy link
Mannequin

devkapilbansal mannequin commented Sep 26, 2021

BPO 45293
Nosy @rhettinger, @ericvsmith, @tirkarthi, @devkapilbansal

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-09-26.20:58:22.124>
created_at = <Date 2021-09-26.12:13:59.531>
labels = ['3.8', 'type-bug', '3.7', '3.9', 'invalid']
title = 'List inplace addition different from normal addition'
updated_at = <Date 2021-09-26.20:58:22.119>
user = 'https://github.com/devkapilbansal'

bugs.python.org fields:

activity = <Date 2021-09-26.20:58:22.119>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2021-09-26.20:58:22.124>
closer = 'rhettinger'
components = []
creation = <Date 2021-09-26.12:13:59.531>
creator = 'devkapilbansal'
dependencies = []
files = []
hgrepos = []
issue_num = 45293
keywords = []
message_count = 4.0
messages = ['402658', '402659', '402660', '402679']
nosy_count = 4.0
nosy_names = ['rhettinger', 'eric.smith', 'xtreak', 'devkapilbansal']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45293'
versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9']

@devkapilbansal
Copy link
Mannequin Author

devkapilbansal mannequin commented Sep 26, 2021

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.

@devkapilbansal devkapilbansal mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels Sep 26, 2021
@ericvsmith
Copy link
Member

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

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

@tirkarthi
Copy link
Member

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__

@rhettinger
Copy link
Contributor

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants