context not cleaned at the end of a loop containing yield?
TP
Tribulations at Paralleles.invalid
Thu Mar 26 14:23:19 EDT 2009
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)
More information about the Python-list
mailing list