[Python-ideas] Integrate some itertools into the Python syntax
Nick Coghlan
ncoghlan at gmail.com
Wed Mar 23 01:13:14 EDT 2016
On 22 March 2016 at 09:06, Michel Desmoulin <desmoulinmichel at gmail.com> wrote:
> So my first proposal is to be able to do:
>
> def stop(element):
> return element > 4
> print(numbers[:stop])
>
> It's quite pythonic, easy to understand : the end of the slice is when
> this condition is met. Any not the strange way takewhile work, which is
> "carry on as long as this condition is met".
>
> We could also extend itertools.islice to accept such parameter.
iter() already has a two-argument form to accept a callable+sentinel
value, so it may not be unreasonable to offer an "until" callback to
say when to stop iterating.
That is:
def stop(element):
return element >4
print(list(iter(numbers, until=stop)))
Slicing arbitrary iterables may be amenable to a str.join style
solution, by putting the functionality on slice objects, rather than
on the iterables:
slice(3, 7).iter(iterable)
(Whether or not to make slice notation usable outside subscript
operations could then be tackled as an independent question)
For itertools.chain, it may make sense to simply promote it to the builtins.
I'm not the least bit sure about the wisdom of the first two ideas,
but the last one seems straightforward enough, and I'd be comfortable
with us putting "chain" on the same tier as existing builtin iteration
related tools like enumerate and zip.
Regards,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list