Hi everybody,

This example gives strange results:

########
def foo( l = [] ):

    l2 = l
    print l2
    for i in range(10):
        if i%2 == 0:
            l2.append( i )
        yield i
########

>>> [i for i in ut.foo()]
[]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [i for i in ut.foo()]
[0, 2, 4, 6, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [i for i in ut.foo()]
[0, 2, 4, 6, 8, 0, 2, 4, 6, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
...

How to explain this behavior? Why l is not [] when we enter again the
function foo()?

Thanks

Julien

-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to