<div dir="ltr"><div>HI everybody,</div><div><br></div>I see that discussion has stalled on this suggestion. What can we do to move this forward?<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Feb 8, 2014 at 10:32 AM, Terry Reedy <span dir="ltr"><<a href="mailto:tjreedy@udel.edu" target="_blank">tjreedy@udel.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">On 2/7/2014 9:28 AM, Serhiy Storchaka wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
07.02.14 10:36, Terry Reedy напиÑав(ла):<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I wrote the above with the idea that there would be a third parameter to<br>
provide an exception. It would have to have a private default like<br>
<br>
class _NoException(Exception): pass<br>
</blockquote>
<br>
Natural default is StopIteration or ().<br>
</blockquote>
<br></div>
That does not work because iter has to know if the user called iter with only one argument, an iterable, or with two or (with the proposal) three, with the first argument being a callable.<br>
<br>
def iter(iter_or_call, sentinel=<private object>, stop_iter=<private exception>):<br>
 if sentinel == <private object> and stop_iter == <private exception>:<br>
  # iter_or_call must be an iterable<br>
  <do what iter(iterable) does now,<br>
   which is to return iterable.__iter__()<br>
   or getitem_iterable(iterable)><br>
 else:<br>
  # iter_or_call must be a callable<br>
  return callable_iterator(iter_or_<u></u>call, sentinel, stop_iter)<br>
<br>
where callable_iterator has been modified to take the third parameter and raise StopIteration if iter_or_call raises stop_iter.<br>
<br>
class Callable_iterator:<br>
 def __next__(self):<br>
  try:<br>
   val = self.func()<br>
  except self.stop_iter:<br>
   raise StopIteration from None<br>
  if self.sentinel == value:<br>
   raise StopIteration<br>
  else:<br>
   return val<br>
<br>
If a user passes sentinel but not stop_iter, the except clause should have no effect and the method should work as it does currently. A default of StopIteration would accomplish that but it does not work for the iter switch. A private exception class also works since no exception raised by self.func should be an instance of such a class (unless the user violates convention).<br>
<br>
If a user passes stop_iter but not sentinel, the if clause should have no effect and the iteration should continue until there is an exception. So the default sentinel should never compare equal to any value returned by self.func. A private instance of object on the left of == will invoke object.__eq__ which compares by identity. Unless a user violates convention by extracting and using the private instance, the function will never return it. Since value could compare equal to everything, the order for == matters.<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Terry Jan Reedy</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
<br>
______________________________<u></u>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/<u></u>codeofconduct/</a><br>
<br>
-- <br>
<br>
--- You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.<br>
To unsubscribe from this topic, visit <a href="https://groups.google.com/d/topic/python-ideas/UCaNfAHkBlQ/unsubscribe" target="_blank">https://groups.google.com/d/<u></u>topic/python-ideas/<u></u>UCaNfAHkBlQ/unsubscribe</a>.<br>
To unsubscribe from this group and all its topics, send an email to <a href="mailto:python-ideas%2Bunsubscribe@googlegroups.com" target="_blank">python-ideas+unsubscribe@<u></u>googlegroups.com</a>.<br>
For more options, visit <a href="https://groups.google.com/groups/opt_out" target="_blank">https://groups.google.com/<u></u>groups/opt_out</a>.<br>
</div></div></blockquote></div><br></div>