Why Python does *SLICING* the way it does??

Antoon Pardon apardon at forel.vub.ac.be
Thu Apr 21 04:18:48 EDT 2005


Op 2005-04-21, Raymond Hettinger schreef <vze4rx4y at verizon.net>:
> [Antoon Pardon]
>> I don't see why the start index can't be accessible through
>> a method or function just like the length of a list is now.
>>
>> My favourite would be a range method so we would have
>> the following idiom:
>>
>>   for i in lst.range():
>>     do something with lst[i]
>
> After going to all that trouble, you might as well also get the value at that
> position:
>
> for i, x in enumerate(lst):
>     do something with lst[i] also known as x

No you wouldn't, enumerate always starts with 0.

So if you write a class with list-like behaviour except that the
start index can be different from 0, enumerate is useless
because lst[i] won't be x in that case.

-- 
Antoon Pardon



More information about the Python-list mailing list