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

Ethan Furman ethan at stoneleaf.us
Tue Mar 22 16:17:26 EDT 2016


On 03/22/2016 12:43 PM, Michel Desmoulin wrote:
 > No you didn't store the lines, the whole points is that they are
 > returning generators.
 >
 > In that context, f[begin, end] does itertools.takewhile and
 > dropwhile and [:10000] is doing islice(0, 10000). None of those
 > functions store anything.
 >
 > Please read the begining of thread.

I presume you are referring to this line:

 > The slicing would then return a list if it's a list, a tuple if it's
 > a tuple, and a islice(generator) if it's a generator.

Of which the big problem is that the object returned from an open call 
is not a list, not a tuple, and not a generator.

For the sake of argument let's pretend you meant "and an islice(iter) 
for everything else".

So the actual proposal is to add `__getitem__` to `object` (that way all 
classes that define their own `__getitem__` such as list and tuple are 
not affected), and the substance of this new `__getitem__` is to:

- check for an `__iter__` method (if none, raise TypeError)
- check the start, stop, step arguments to determine whether
   dropwhile, takewhile, or islice is needed
- return the appropriate object

Okay, it's an interesting proposal.

I would suggest you make the changes, run the test suite, see if 
anything blows up.

As a short-cut maybe you could add the code to the appropriate ABC and 
test that way.

--
~Ethan~


More information about the Python-ideas mailing list