Finn Mason writes:

 > We don't need people writing `next(iter(iterable))` just to get the
 > first item.

We already don't need that.  `sequence[0]` and `next(iterator)` do the
trick.

You only need `next(iter(iterable))` if you need all three of

- an expression (`for first in iterable: break` gives the other two),
- efficient (although the difference with the `for` idiom should
  usually be irrelevant)
- polymorphic over sequences and iterators (both special notations are
  more efficient expressions).

and it makes it explicit that that's what you're after.

At least for beginners, putting this idiom in the Tutorial is probably
the best idea.  With the exception of wacko iterables that define
`__next__` but not `__iter__` (which `first` could take care of, but
do we really want to do that? I guess we should if we're going to
define `first`), not only is the idiom the efficient polymorphic
expression, but to understand why it works teachs a lot about
iterables vs. iterators vs. sequences.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5LQ5TIEDK3PGYB5CYD4GZ7ZMZE7YVWXY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to