On Tue, Dec 10, 2019 at 1:37 AM Andrew Barnert <abarn...@yahoo.com> wrote: > > On Dec 9, 2019, at 22:07, Wes Turner <wes.tur...@gmail.com> wrote: > > > > > > Sets are not collections.abc.Sequence because they do not implement > > __getitem__. > > Are there other unordered Iterables in the standard library > > Again, it depends on what you’re trying to distinguish by that word > “unordered”. Mappings (including dict) and their views, Sets (including > frozenset), Iterators (including file objects, map, filter, generators, and > most itertools functions), and many other things are Iterables without being > Sequences. I have no idea which if any of these you mean by “unordered”. For > every single one of them, just like for set, first(it) will give you the same > value as list(it)[0] (albeit a different exception if they’re empty), so I > don’t see why any of them are confusing. What else would you expect first to > do? And, without knowing why you think first should mention any of them, I > have no idea which ones it should mention.
(naievely) 'first' would seem to imply a stable ordering that doesn't change with PYTHONHASHSEED='random' / -R (the default since 3.3) The .first() docstring could mention that sets, in particular, may have a different ordering for different interpreter invocations (when the elements are integers less then sys.hash_info.modulus). > > >> On Tue, Dec 10, 2019 at 12:44 AM Tim Peters <tim.pet...@gmail.com> wrote: > >> > >> ... as is my _current_ idiom: > >> > >> for a in iterable: > >> break > >> else: > >> raise ... > > > > Is this pattern in the tutorial? > > No, but you will find it on StackOverflow. I don’t think it needs to be in > the tutorial. If needing to raise a specific exception—or just not > StopIteration—is common enough that novices need to learn it, I think it’s > common enough that it should be given a name and put in the itertools recipes > or module, or at least pointed out in more-itertools, rather than teaching > people to spell it out every time. > > >> Plain old > >> > >> a = first(iterable) > >> > >> would be perfect - but only if it raised. > > > > +1. How is this distinct from: > > > > first = next > > Because first works with any Iterable; next works only with iterators. def first(iterable, *args): return next(iter(iterable), *args) first.__doc__ = (next.__doc__.replace('next(', 'first(') + """ .. note:: func:`next` takes a second argument as a default value .. warning:: first(set('abc')) will only sometimes return 'a'""") _______________________________________________ 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/JXHSCXXI26SGKALFQDMZ3KD3FWUOWE6D/ Code of Conduct: http://python.org/psf/codeofconduct/