On Sat, Dec 14, 2019 at 2:50 AM Stephen J. Turnbull <turnbull.stephen.fw@u.tsukuba.ac.jp> wrote:
Did you intend to reply only to me?

Nope - I hate list that don’t default to replying to the list (I know that’s an unpopular opinion).

I was wondering why no one comment on it. 

 > I’m quite convinced that first() would NOT cause any particular
 > confusion, neither for sets nor iterators.

I don't see how you make that judgment so confidently, since you don't
claim experience with `first`, only the educationally problematic
`next(iter())`.

Well, that’s why I said *I’m* convinced, rather than state it as a general truism.

I have a lot of experience with newbies, so I think I have a pretty good idea what tends to trip them up. I could be wrong of course, but without actually introducing it using it in and a couple of years worth of teaching, we’ll never know for sure :-)

I don't agree.  I can't speak for others, but once I understood the
difference between "iterable" and "iterator", "next(iter(iterable))"
was self-explanatory when I first encountered it.

Sure — but the newbies I’m talking about don’t yet understand the difference between "iterable" and "iterator", and many people get very far before they do. 

I *think* the primary target audience for first() would be those folks, so it’s kind of ironic that we’re concerned about confusing them.

> I don't see how you
explain all that in terms of first() without the concept of iter(),
and the detail that (iter(iterator)) == iterator.

I don’t think you need to — that’s my whole point. In common code, folks rarely work with bare iterators (usually they are implicitly created in a for loop, for instance), so that distinction doesn’t come up. 

And when it *does* come up, it applies to every use of the iteration protocol: for loops, next, etc. so nothing special about first()

I think I made that point already, probably in a note sent to only one person by mistake. 

Of the top of my head, I can’t think of a single non-contrived example of using a bare iterator in case that is not specifically doing something “special”.

Can anyone else?

Sure, you can
*tell* students that's how first() works, but how do they apply it in
their own code?

You tell them that can use it to get the first item out of a container or other iterable. And you’re done.

Some time in the future, they may encounter a bare iterator, and surprised that first() consumes an item. But as I said before, they’ll have other issues as well, and will need to learn about iterators then.

-CHB 


--
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython