Seek support for new slice syntax PEP.

Dave Angel davea at ieee.org
Sat Dec 19 10:53:10 EST 2009


Colin W. wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">On 
> 18-Dec-09 23:16 PM, Nobody wrote:
>> On Fri, 18 Dec 2009 09:49:26 -0500, Colin W. wrote:
>>
>>> You don't say, but seem to imply that the slice components include 
>>> None.
>>
>> That's how missing components are implemented at the language level:
>>
>> > class foo:
>> = def __getitem__(self, s):
>> = return s
>> =
>> > x = foo()
>> > x[::]
>> slice(None, None, None)
>> > x[1::2]
>> slice(1, None, 2)
>>
>> The defaults of zero, sys.maxint and one apply to built-in types, but
>> nothing forces user-defined types to behave this way.
>>
>> Or maybe I misunderstood your point.
>>
> No, it seems that the implementation is a little different from the doc.
>
> You are right:
> *** Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 
> bit (Intel)] on win32. ***
> >>> a= range(10)
> >>> a[2:8:2]
> [2, 4, 6]
> >>> a[2::2]
> [2, 4, 6, 8]
> >>> a[2:None:2]
> [2, 4, 6, 8]
> >>>
> I had expected the last to be rejected, but it fits with the overall 
> philosophy.
>
> Colin W
>
> </div>
>
None is perfectly valid as a parameter to a slice. To quote the 2.6.4 
docs, in section 6.6:

The slice of /s/ from /i/ to /j/ with step /k/ is defined as the 
sequence of items with index x = i + n*k such that 0 <= n < (j-i)/k. In 
other words, the indices are i, i+k, i+2*k, i+3*k and so on, stopping 
when /j/ is reached (but never including /j/). If /i/ or /j/ is greater 
than len(s), use len(s). If /i/ or /j/ are omitted or None, they become 
“end” values (which end depends on the sign of /k/). Note, /k/ cannot be 
zero. If /k/ is None, it is treated like 1.


DaveA




More information about the Python-list mailing list