Strange generator problem

Joost Cassee joost at cassee.net
Fri Oct 5 11:23:57 CEST 2007


Hello all,


I bumped into an unexpected problem using generators. Consider this code:

def test(s):
    return _test([], [], s)
def _test(s1, s2, s):
    if s:
        s1.append(s.pop())
        for x in _test(s1, s2, s):
            yield x
        s2.append(s1.pop())
        for x in _test(s1, s2, s):
            yield x
        s.append(s2.pop())
    else:
        yield s1, s2

The test function is supposed be a generator returning all combinations
of dividing the elements of the input list into two distinct lists
(order of elements unimportant). (This post is not about the quality of
the solution. :-) )

Consider now the result of using the generator in a for-loop and in the
list function (same result for list comprehension):

>>> for x in test(range(3)):
...      print x
...
([2, 1, 0], [])
([2, 1], [0])
([2, 0], [1])
([2], [1, 0])
([1, 0], [2])
([1], [2, 0])
([0], [2, 1])
([], [2, 1, 0])
>>>
>>> list(test(range(3)))
[([], []), ([], []), ([], []), ([], []), ([], []), ([], []), ([], []),
([], [])]

The following (simpler) generator works as expected:

>>> def test2(s):
...     for x in s:
...         yield x
...
>>> list(test2(range(3)))
[0, 1, 2]

Can anyone explain the difference? The python version is 2.5.1.


Regards,

Joost



More information about the Python-list mailing list