
сб, 7 дек. 2019 г. в 03:45, Steven D'Aprano <steve@pearwood.info>:
This is how I would implement the function in Python:
def first(iterable, default=None): return next(iter(iterable), default)
A somewhat related discussion was somewhere inside November 2017 <https://mail.python.org/archives/list/python-ideas@python.org/thread/QX6IRM4...> thread "How assignment should work with generators?". The main idea was to extend assignment syntax to something like `first, second, ... = gen` which should be equivalent to `first, second, ... = next(gen), next(gen)`. Another idea was (but nobody liked it) to change the behavior of `x, y, *tail = iter` to not to consume starred part and to make a generator instead of list. I understand that the topic discussed here is slightly different, but in my understanding it is all the eggs of one chicken. I still like the idea of partial assignment syntax `x, y, ... = iter` but as well as then, I understand that I don’t have enough English knowledge and time to write a proposal on that topic and especially to follow thread and to defend an idea :( The main objection was that this all is easy achievable with `islice` from `itertools`. Back to the topic - maybe `first` is a good citizen for `itertools` module. Because this idea (about first ) is discussed constantly every two years. with kind regards, -gdg