[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
Mark Dickinson added the comment: Another 'fix' would be to allow assignment to a tuple item in the case that the item being assigned is the same (in the sense of 'is') as the item already in the tuple. Then the '+=' operation would succeed, but the tuple would remain immutable. -- nosy: +mark.dickinson ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
Mark Dickinson added the comment: @andy.chugunov: tuples are immutable in the sense that you can't put a new object into a tuple or remove objects from a tuple. That doesn't mean that tuples can't contain mutable objects, or prevent you from mutating the objects inside a tuple. So the append method call is fine: it's not modifying the tuple itself (the tuple still has references to exactly the same objects both before and after the append call); it's merely mutating one the objects inside the tuple. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
R. David Murray added the comment: This gets reported periodically. I wonder if it is time for a FAQ entry. There doesn't seem to be one yet. -- nosy: +r.david.murray ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
Ronald Oussoren added the comment: You've got a point there. What about this patch (but then with proper english grammer)? BTW. Actually fixing this wart would be possible, but at a significant cost: you'd have to change the implementation of LHS += RHS from: tmp = LHS tmp = tmp.__iadd__(RHS) LHS = tmp to: tmp = LHS LHS = tmp tmp = tmp.__iadd__(RHS) LHS = tmp The odd assignment on the second line would detect that the LHS is immutable in 99+% of use cases before updating the RHS. My gut feeling is that an implementation of this would have too high a cost (both in runtime performance and in a more complicated compiler), although it would be interesting to actually see a patch. -- keywords: +patch Added file: http://bugs.python.org/file30294/faq-update.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
Andy Chugunov added the comment: Thank you for the clarification! The exception is appropriate as tuples have to stay immutable. Got it. Could you please also explain a bit about the append() call? Should it in theory raise an exception as well or is such clean behavior intended? -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
New submission from Andy Chugunov: At the same time append() succeeds silently, while simple '+' fails. Here's an example: a = ([1],) a[0].append(2) a ([1, 2],) a[0] += [3] Traceback (most recent call last): File pyshell#47, line 1, in module a[0] += [3] TypeError: 'tuple' object does not support item assignment a ([1, 2, 3],) a[0] = a[0] + [4] Traceback (most recent call last): File pyshell#49, line 1, in module a[0] = a[0] + [4] TypeError: 'tuple' object does not support item assignment a ([1, 2, 3],) Looks like a self-contradictory behavior. Unfortunately, I'm not yet advanced enough to figure out where the problem might be and submit a fix. Tested with v3.3.1 on Windows 7 (64-bit), and v3.2.3 and v2.7.3 on Debian 7 (also 64-bit). -- messages: 189201 nosy: andy.chugunov priority: normal severity: normal status: open title: '+=' on a list inside tuple both succeeds and raises an exception type: behavior versions: Python 2.7, Python 3.3 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
Changes by Ronald Oussoren ronaldousso...@mac.com: -- assignee: - ronaldoussoren ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
Ronald Oussoren added the comment: This is a side effect to the way the in place operators work. Basically a[0] += [3] is evaluated as: a[0] = a[0].__iadd__([3]) The call to __iadd__ succeeds, which is why the list is updated, but you get an exception when the interpreter tries to update item 0 of the tuple. -- nosy: +ronaldoussoren ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
Ronald Oussoren added the comment: I'm closing this issue as rejected because the current behavior is intentional, although it is confusing (IMO). -- resolution: - rejected status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17973] '+=' on a list inside tuple both succeeds and raises an exception
Changes by Florent Xicluna florent.xicl...@gmail.com: -- nosy: +flox ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17973 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com