A usage example that will lead me to the search is as follows:
def setfrom(self,f): """Set from iterable f""" fi = iter(f) for i in range(self.n): try: x = next(fi) except StopIteration: fi = iter(f) x = next(fi) self.a[i] = x
where hasnext () is available, you can do
def setfrom(self,f): """Set from iterable f""" fi = iter(f) for i in range(self.n): if not hasnext(fi): fi = iter(f)
which is cleaner for me. Obviously, you can solve problems by setting utility classes, but then it happens that you have twenty-four different almost equivalent workarounds, each of which is related to their quirks, and if you want to reuse code that uses different workarounds, you you need to either have several close to the equivalent in one application, or bypass the selection and rewriting of code to use the same approach. “Do it once and do it well,” the maximum fails.
In addition, the iterator itself must have an internal hasnext check to see if it needs to throw an exception. This internal check is then hidden so that it needs to be tested, trying to get the element, catching the exception and running the handler if it was thrown. This does not need to hide IMO.
John Allsup Jan 12 '14 at 16:49 2014-01-12 16:49
source share