syntax enhancement

Terry Reedy tjreedy at udel.edu
Wed Oct 5 16:09:14 EDT 2011


On 10/5/2011 2:31 PM, Tim Chase wrote:
> On 10/04/11 20:45, Terry Reedy wrote:
>> On 10/4/2011 9:50 AM, Valiev Sergey wrote:
>>
>>> - `[]` - used for list comprehension,
>>> - `()` - used for generators,
>>> - `[start:stop]` / `[start:stop:step]` - used for slices.
>>> The idea is to use `(start:stop)` / `(start:stop:step)` as 'lazy
>>> evaluated' slices (like itertools.islice).
>>> What do you think about it?
>>
>> a(b) is already used for function calls. Making a(b:c) be something
>> unreleated does not seem like a good idea to me. At present, a[b:c] ==
>> a[slice(b,c)]. However, a(slice(b,c)) is already a function call and
>> could not equal a(b:c).
>
> I'm very -1 on the initial proposal with parens, but I wouldn't object
> to generators growing a method (__getitem__?) to do slices via
> itertools, something like
>
> gen = (a for a in iterator if test(a))
> for thing in gen[4::2]:
> do_something(thing)
>
> acting something like
>
> gen = (a for a in iterator if test(a))
> for thing in itertools.islice(gen, start=4, step=2):

'end' arg is required

> do_something(thing)

islice(gen,4,None,2) is 11 more characters than
gen[4::2]

If you start with 'from itertools import islice as sl', then each use is 
7 more chars. In the normal case with an 'end' arg, the difference would 
be 4 chars less:

sl(gen,4,100,2)  # 3 extra chars to type ;=)
gen[4:100:2]

This would complicate the language and make generators more different 
from other iterators without adding new functionality

-- 
Terry Jan Reedy




More information about the Python-list mailing list