On Sat, 13 Jun 2015 13:48:45 +0100, Oscar Benjamin wrote: > On 13 June 2015 at 08:17, Steven D'Aprano > <steve+comp.lang.pyt...@pearwood.info> wrote:
>> But an easier way is: >> >> Test = [1, 2] >> Test.extend(Test) >> print(Test) > > I can't see anything in the docs that specify the behaviour that occurs > here. Neither do I, but there is a test for it: a.extend(a) self.assertEqual(a, self.type2test([0, 0, 1, 0, 0, 1])) https://hg.python.org/cpython/file/a985b6455fde/Lib/test/list_tests.py > If I change it to > > Test.extend(iter(Test)) > > then it borks my system in 1s after consuming 8GB of RAM (I recovered > with killall python in the tty). The reason that fails should be obvious: as new items keep getting added to Test, the iterator likewise sees more items to iterate over. I don't know if this is documented, but you can see what happens here: py> L = [10, 20] py> it = iter(L) py> L.append(next(it)); print L [10, 20, 10] py> L.append(next(it)); print L [10, 20, 10, 20] py> L.append(next(it)); print L [10, 20, 10, 20, 10] py> L.append(next(it)); print L [10, 20, 10, 20, 10, 20] So as Test.extend tries to iterate over iter(Test), it just keeps growing as more items are added to Test. -- Steven D'Aprano -- https://mail.python.org/mailman/listinfo/python-list