On Wed, Aug 24, 2016 at 9:03 PM, Joaquin Alzola <joaquin.alz...@lebara.com> wrote: >>> One might guess a.extend(a) would turn into an infinite loop. It turns out >>> here Python first gets all the items of `a' and then append them to `a', so >>> the infinite loop is avoided. >>>> a = [1,2] >>>> for x in a: a.append(x) >>... >>^CTraceback (most recent call last): > > File "<stdin>", line 1, in <module> >>KeyboardInterrupt >>>>> len(a) >>6370805 > >>That right there, folks, is an infinite loop. > > If I am correct python points out an infinite loop with the "...", just > pointing to more information.
That's true of self-referential objects and circular references: >>> a = [1, 2] >>> a.append(a) >>> a [1, 2, [...]] In this case, it's not an infinite loop or anything; it's simply an object that references itself: >>> id(a) 139945904550344 >>> [id(x) for x in a] [9241344, 9241376, 139945904550344] The list has three elements, one of which is the list itself. Same applies if the list has a reference to something else which has a reference to the original list, or anything along those lines. But "a.extend(a)" isn't quite like that. In its purest form, it means the same as the 'for' loop that I showed above, but... well, this comment from the CPython sources says exactly what I'm thinking of: Objects/listobject.c:795 /* Special cases: 1) lists and tuples which can use PySequence_Fast ops 2) extending self to self requires making a copy first */ > This email is confidential and may be subject to privilege. If you are not > the intended recipient, please do not copy or disclose its content but > contact the sender immediately upon receipt. > This email is confident and may be subject to white privilege, black privilege, and privileges of other colours. If you are not the intended recipient, please contact a lawyer and tell him that email footers are unenforceable. ChrisA -- https://mail.python.org/mailman/listinfo/python-list