Interesting list() un-optimization
Terry Reedy
tjreedy at udel.edu
Thu Mar 7 15:29:00 EST 2013
On 3/7/2013 11:00 AM, Ian Kelly wrote:
> But on this point, you are correct. The mongoengine QuerySet.__iter__
> method is defined as:
>
> def __iter__(self):
> self.rewind()
> return self
>
> This is unfortunate design. Not only does it mean that the iterator's
> __len__ method cannot be trusted (what should the __len__ of a
> partially exhausted iterator return?), but it also means that requesting
> an iterator over the QuerySet will also silently invalidate any
> existing iterators.
I view that design as a violation of the iterator protocol and hence a
program bug. __iter__ should either *just* return self (if the self is
an iterator) or return a new object (if self is a non-iterator
iterable). File objects are iterators and .__iter__ does not rewind.
>>> f = open("f:/python/mypy/tem.py")
>>> next(f)
'class myit(list):\n'
>>> f2 = iter(f)
>>> f2 is f
True
>>> next(f2)
" def __bytes__(self): return b'hello'\n"
--
Terry Jan Reedy
More information about the Python-list
mailing list