jeudi 13 août 2015

python reverse iterator vs. generator behavior

Why does removing items a list break reversed objects? It doesn't break gen-exprs, and appending to or modifying the list doesn't break the reversed object, and it obviously points to the original object, so why can't it give a truncated version? Perhaps some examples can clarify:

l = [1, 2, 3, 4]
r = reversed(l)
g = (i for i in l)
l.pop()  # returns 4
l   # returns [1, 2, 3]

for i in g:print(i)  # prints 1 2 3 (on separate lines)
for i in r:print(i)  # prints ...nothing

r = reverse(l)
g = (i for i in l)
l[1] = 4
for i in g:print(i)  # prints 1 4 3 (on separate lines)
for i in r:print(i)  # prints 3 4 1 (on separate lines)

r = reversed(l)
g = (i for i in l)
l.append(5)
l  # returns [1, 4, 3, 5] just to keep you on your toes

for i in g:print(i)  # prints 1 4 3 5 (on separate lines)
for i in r:print(i)  # prints 3 4 1   (on separate lines)

So - if the genexpr is smart enough to point to the object, and just respond to however the object has changed, why doesn't reversed? It obviously doesn't make a copy, otherwise it wouldn't "fail" on the first situation, and it wouldn't pick up the 4 in the second. So it must point to the object. Why can't it just start from index -1 and work backwards?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire