Pylint say
W: 6: Using possibly undefined loop variable 'n'
with this code:
iterator = (i*i for i in range(100) if i % 3 == 0)
for n, i in enumerate(iterator):
do_something(i)
print n
because if the iterator is empty (for example []) n is undefined, ok. But I like this trick. How to use it in a safe way?
I think that using len(list(iterator)) is not the best choice because you have to do two loops. Using a counter, and incrementing it I think it's not very pythonic.