]> granicus.if.org Git - python/commitdiff
Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
authorMark Dickinson <dickinsm@gmail.com>
Mon, 26 Oct 2009 14:18:44 +0000 (14:18 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Mon, 26 Oct 2009 14:18:44 +0000 (14:18 +0000)
Python/bltinmodule.c

index 1f25d5d2087a301739b5b38fd50020742e550592..c0607706dcf3ba2bfb6ad7c4953480174193d515 100644 (file)
@@ -2350,6 +2350,15 @@ builtin_sum(PyObject *self, PyObject *args)
                        }
                        break;
                }
+               /* It's tempting to use PyNumber_InPlaceAdd instead of
+                  PyNumber_Add here, to avoid quadratic running time
+                  when doing 'sum(list_of_lists, [])'.  However, this
+                  would produce a change in behaviour: a snippet like
+
+                    empty = []
+                    sum([[x] for x in range(10)], empty)
+
+                  would change the value of empty. */
                temp = PyNumber_Add(result, item);
                Py_DECREF(result);
                Py_DECREF(item);