[Python-ideas] [Python-Dev] Inclusive Range
Terry Reedy
tjreedy at udel.edu
Tue Oct 12 13:50:47 EDT 2010
On 10/12/2010 9:52 AM, Antoon Pardon wrote:
> On Fri, Oct 08, 2010 at 05:05:26PM -0400, Terry Reedy wrote:
>> But you really seem to be saying is "What if I sometimes want the
>> end points included and sometimes do not?" Slice syntax by itself
>> cannot handle all four cases, only one, one was chosen and that was
>> closed-open.
>>
>> If you want flexibility, consider the following:
>>
>> class my_list(list):
>> def __getitem__(self, key, include_start=True, include_stop=False):
Sorry, the above is a holdover from a previous, experimental version. In
this version, the extra parameters should be eliminated.
This should be just the expected
def __getitem__(self, key):
>> if (isinstance(key,tuple) and len(key)==2 and
>> isinstance(key[0], slice)
>> and isinstance(key[1],tuple) and len(key[1])==2):
>> key, (include_start, include_stop) = key
Here I unconditionally set the two include variables from the key.
The default values are ignored and are pure holdover noise.
>> start,stop,stride = key.indices(len(self))
>> if include_start == False:
>> start += 1
>> if include_stop == True:
>> stop += 1
>> key = slice(start,stop,stride)
>> print(key)
>> return list.__getitem__(self, key)
>>
>> ll = my_list(range(10))
>
> That seems to be an undocumented feature. I didn't know it was possible
> to use extra parameters after key in __getitem__.
They never get passed, and as I said above, should not have been there
in the version I posted. Sorry for the noise. The actual point is that
keys can be tuples and tuples can contain a slice and other info.
--
Terry Jan Reedy
More information about the Python-list
mailing list