Django/Python Epix Phail #5 - don't be evil.
Every language has its rough spots.
Ruby for instance does not handle array slices very nicely.
>> a = [1, 2, 3]
=> [1, 2, 3]
>> a[0]
=> 1
>> a[0..1]
=> [1, 2]
>> a[2, 3]
=> [3]
>> a[3, 4]
=> []
>> a[4, 5]
=> nil
Now, thats evil. Why should a[3, 4] give you an empty array (which is expected) but a[4, 5] give you nil!
But, Ruby gets some things right.....
>> a='1,2,3' See that? an empty string gets you an empty array.
=> "1,2,3"
>> a.split(',')
=> ["1", "2", "3"]
>> a = '1'
=> "1"
>> a.split(',')
=> ["1"]
>> a = ''
=> ""
>> a.split(',')
=> []
>>
Theoretically, it SHOULD give you an array with a single empty text element, but it is nicer than that.
Not Evil.
Python on the other hand....
>>> a = '1,2,3'
>>> a.split(',')
['1', '2', '3']
>>> a = '1'
>>> a.split(',')
['1']
>>> a = ''
>>> a.split(',')
['']
Evil! pure and simple.
And lets look at slicing....
>>> a=(1,2,3,)
>>> a[0:1]
(1,)
>>> a[0:2]
(1, 2)
>>> a[0:3]
(1, 2, 3)
>>> a[0:4]
(1, 2, 3)
>>> a[1:4]
(2, 3)
>>> a[2:4]
(3,)
>>> a[3:4]
()
>>> a[4:5]
()
>>> a[6:7]
()
>>> a[7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
In some ways that is better, slicing works nicely, but indexing a single element raises an index error.
Surely, if it is happy to slice the array outside of its limits and return a valid result, why not handle
accessing an element outside of the limits too?