* Georg Dahn <[EMAIL PROTECTED]> [060423 01:39]:
> Hi!
> 
> Well, you are right that this is at least from a mathematical point of
> view correct. I have given this a second thought and you seem to be
> right, that, if a[M:N] for M or N < len(a) returns an empty list, many
> problems can be solved more elegantly, and probably this is more
> interesting than possible negative effects of this behavior.
> 
> So, the better arguments seem to be on your side. But in whatever way
> this is implemented, IMHO it should be consistent, which isn't the case
> at the moment.
> 
> Best wishes,
> Georg
> 

I agree with Georg that the edge conditions should behave consistently.
Handling an empty list in the same way that a non-empty list is handled
is important.  The following should always work, regardless of whether
list is empty or not:

echo list[0:len(list)-1]

If list is empty, len(list)-1 becomes -1, and this becomes

echo list[0:-1]

which works now for all lists except empty lists.  If

echo list[2:10]

works for a list of five elements (and I am not convinced that this is a
good thing), then either

echo list[-1, 3]

or

echo list[0, 3]

should give an empty list if list is empty.  Neither of these, however,
is consistent with the requirement that M or N must be a valid index.

I think that if the first expression, list[0,-1], works for all lists,
empty or not, then it is not necessary to allow  list[M:N]  where M is
a valid index but N is not.

Allowing invalid indexes is not what simplifies programming expressions;
it is handling the edge conditions consistently, so that there are
simple expressions that allow you to get at empty and non-empty lists
from both the front and back.  Allowing 0 as the starting index and -1
as the ending index, even when the list is empty, solves this problem
elegantly and consistently.  You can then count on using 0 to get the
start of the list and both len()-1 and -1 to get the end of the list,
even if the list is empty.  You can still only get an empty list from an
empty list, but that is how it should be.  For an empty list, the only
valid index expression of the form [M:N] should be [0:-1].

...Marvin

Reply via email to