[Python-ideas] Integrate some itertools into the Python syntax

Andrew Barnert abarnert at yahoo.com
Wed Mar 23 13:39:28 EDT 2016


On Mar 23, 2016, at 10:13, Michael Selik <mike at selik.org> wrote:
> 
>> On Mar 23, 2016, at 6:37 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> 
>> On Wed, Mar 23, 2016 at 9:04 PM, Michel Desmoulin
>> <desmoulinmichel at gmail.com> wrote:
>>>> (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.
>>> 
>>> Same problem as with new keywords : it can be a problem with people
>>> using chain as a var name.
>> 
>> Less of a problem though - it'd only be an issue for people who (a)
>> use chain as a var name, and (b) want to use the new shorthand. Their
>> code will continue to work identically with itertools.chain (or not
>> using it at all). With a new keyword, their code would instantly fail.
> 
> I enjoy using ``chain`` as a variable name. It has many meanings outside of iteration tools. Three cheers for namespaces.

As Chris just explained in the message you're replying to, this wouldn't affect you. I've used "vars" and "compile" and "dir" as local variables many times; as long as I don't need to call the builtin in the same scope, it's not a problem. The same would be true if you keep using "chain". Unless you want to chain iterables of your chains together, it'll never arise.

Also, putting things in builtins isn't _always_ bad. It's just a high standard that has to be met. I don't think anyone believes any, all, and enumerate fail to meet that standard. So the question is just whether chain meets it.

My problem is that I'm not sure chain really does meet it. It's chain.from_iterable that I often see people reaching for and not finding, and moving chain to builtins won't help those people find it. (This is compounded by the fact that even a novice can figure out how to do chain given chain.from_iterable, but not the other way around.) Also, for something we expect novices to start using as soon as they discover iterators, it seems worrisome that we'd be expecting them to understand the idea of a static method on something that looks like a function but is actually a type before they can have a clue of what it means.

In another thread a year or two ago, someone suggested making chain.from_iterable into a builtin with a different name, maybe "flatten". But that now means we're adding _two_ builtins rather than one, and they're very closely related but don't appear so in the docs, which obviously increases the negatives...

Still, I like adding chain (and/or flatten) to builtins a lot more than I like adding sequence behavior to some iterators, or adding a whole new kind of function-like slicing syntax to all iterables, or any of the other proposals here.


More information about the Python-ideas mailing list