Thanks for the comment and discussion guys.

Bottom line, I'm going to have to remove this pattern from my code:

  foo = (foo for foo in foos if foo.bar).next()

I used to have that a lot in cases where not finding at least one
valid foo is an actual fatal error. But using StopIteration to signal
a fatal condition becomes a bug when interacting with list() as shown
in the original post.

It would be nice if there was a builtin for "get the first element in
a genexp, or raise an exception (which isn't StopIteration)", sort of
like:

  from itertools import islice

  def first_or_raise(genexp):
      L = list(islice(genexp, 1))
      if not L:
          raise RuntimeError('no elements found')
      return L[0]

I also think Jean-Paul's had a good point about how the problems in
the  list/genexp interaction could be addressed.

Thank you,

  -- Tom
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to