On Thu, Dec 12, 2019 at 11:35 PM Serhiy Storchaka <storch...@gmail.com>
wrote:

> 11.12.19 10:45, Steven D'Aprano пише:
> > The thing is, we're fooled by the close similarity of iteration over
> > iterators and other iterables (sequences and containers). Destructive
> > iteration and non-destructive iteration is a big difference. Utility
> > functions like the proposed `first` that try to pretend there is no such
> > difference are, I believe, a gotcha waiting to happen.
>
> This is a good argument against first().


But this is true all across Python -- probably for historical reasons, you
can generally use either an iterator or iterable in the same context, and
the "destructive" nature will be different. Even for loops, which I'm sure
we all agree are going to be used by EVERY python programmer:

In [21]: my_list = [3,4,5,6,7,8]


In [22]: my_iterator = iter(my_list)


In [23]: for i in my_list:
    ...:     print(i)
    ...:

3
4
5
6
7
8

In [24]: for i in my_list:
    ...:     print(i)
    ...:

3
4
5
6
7
8

In [25]: for i in my_iterator:
    ...:     print(i)
    ...:

3
4
5
6
7
8

In [26]: for i in my_iterator:
    ...:     print(i)
    ...:


# Hey what happened to the contents????

My point is that the distinction between an iterable and iterator is
potentially going to bite people in most contexts in Python -- there's
nothing special about the proposed first() in this regard.

The other key thing to remember is that in most contexts, folks are working
with iterables, not iterators(directly) anyway, which is why this does not
constantly mess up novices.

This thread has gotten pretty out of hand (well, not more than many on this
list :-) ) -- we don't NEED to get into the whole theory of what itertools
is for, functional programming, etc -- there is a simple question on the
table:

Do we add a "first()" function to the standard library? And if so, where do
we put it, How exactly should it work?

On that second point, I think we all agree that it does not belong in
__builtins__, so it needs to go somewhere, and itertools seems to make the
most sense. Not because it is a "building block[s] of an algebra for
iterators ", but because no one has suggested another place to put it.

I can see telling folks:

If you want to get the first item from a container or other iterable, you
can use the itertools.first() function to do that.

I suppose they *might* get confused when they read about "an algebra for
iterators", or even the first line in the module docstring: "Functional
tools for creating and using iterators.", but I doubt it -- I suspect
they'll only read the docs for the function itself anyway. And it IS a
"tool for using iterators" so is it reallyTHAT confusing?

-CHB

-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
_______________________________________________
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/BOZVOGBXHDR76RUDHZFWRMKH3TTM45FD/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to