
[Tim]
BTW, you can go a long way in Python without knowing anything about `iter()` or `next()`. But not without mastering `for` loops. That's why I prefer to say that, for ordinary cases,
a = first(it)
has the same effect as:
for a in it: break
[Andrew Barnert]
I just had to explain to someone today that a loop like this doesn’t just use up and throw away a value from it, but leaves a bound to that first value.
Admittedly, this wasn't a novice, but a pretty experienced C++ and Java developer who doesn’t do much Python, who just didn’t know that Python scopes are functions rather than all compound statements. That’s not a confusion a novice would have.
But would a novice actually get that it is usefully assigning the first thing in it to a? It seems obvious to me, but I’m not sure it would to most people who don’t know enough Python to know about iter.
I couldn't care less whether things are "obvious" at first - unless they're Dutch ;-) Nothing about programming is obvious at first. What I do care about is explanations that "stick" _after_ someone makes the effort to learn them.
For that matter, even if they do know this leaves a bound to something useful, do they know what it does with a set? If not, what have we solved here?
You seriously want to claim that it's A Mystery to people what iterating over a set does? That even a newbie would be baffled by the output of: for a in {1, 2, 3}: print(a) ? They may indeed be baffled by the _order_ in which 1, 2, and 3 are printed, but not in the slightest by that exactly those 3 values _are_ printed.
And do we even really need to solve this problem How many novices are going to be confused about what first does with a set, and need to know the answer?
My original statement was "you can go a long way in Python without knowing anything about `iter()` or `next()`, which goes far beyond novices. I didn't even mention newbies.
Plus, if first really has to be this completely understandable to people who don’t know about iter, how can we put it in itertools, a module whose docs start off with a nice friendly introduction about the building blocks of an algebra for iterators that you can compose into powerful tools?
As above, I didn't really have newbies in mind. I did have the learning curve all Python programmers need to climb in mind. `next()` and `iter()` didn't even exist in earlier versions of Python, yet we somehow managed ;-) They've _become_ basic to explaining how (among other things) `for` is _implemented_ now, but it's easy to play with `for` all by itself to figure out how `for` works. And I can't imagine a Python course that even mentioned `iter()` or `next()` before covering `for`. I also said "next(iter(it))" should also be given as a more succinct explanation for "advanced" users - but _expect_ that the "for" explanation would be more accessible to more users. Why are you so irked at an attempt to give "the simplest explanation that could possibly work"?