E
ernest
Hi,
What is better:
def __iter__(self):
for i in len(self):
yield self
or
def __iter__(self):
return iter([self for i in range(len(self))])
The first one, I would say is more correct,
however what if in a middle of an iteration
the object changes in length? Then, the
iterator will fail with IndexError (if items
have been removed), or it will fail to iterate
over the whole sequence (if items have
been added).
What do you think?
Cheers.
Ernest
What is better:
def __iter__(self):
for i in len(self):
yield self
or
def __iter__(self):
return iter([self for i in range(len(self))])
The first one, I would say is more correct,
however what if in a middle of an iteration
the object changes in length? Then, the
iterator will fail with IndexError (if items
have been removed), or it will fail to iterate
over the whole sequence (if items have
been added).
What do you think?
Cheers.
Ernest