4 ms·
No it doesn't. That just gives you the last element, which is not a "based indexing". JavaScript will allow arr[-1] as an "individual" element, but it's intern
by irjustin 6y ago
No it doesn't. That just gives you the last element, which is not a "based indexing".
JavaScript will allow arr[-1] as an "individual" element, but it's internally an object and so iterating and hoping that -1 comes in order doesn't work.
- silon42 6y ago> No it doesn't. That just gives you the last element, which is not a "based indexing". I never liked that feature, it has chance to hide bugs in more complicated code where indexes are calculated... I'd prefer a 'reverse' indexer instead.
- klyrs 6y agoSure it does. -2 gives second to last, -3 gives third to last, etc. If you only use negative indices i, the "base" of the array is the end, and arr[i] is just arr[len(arr)+i].
- irjustin 6y agoHow does that fall within the definition of "base index"? that's just array position referencing. You can't even create an array/list in python w/o a hard 0 reference.
- klyrs 6y agoFirst, it's a "joke", second, nobody is going to force you to use non-negative indices. Sure, you can use a base index of zero and read left to right... but you can use a base index of -1 and read right to left and live a full (if lonely) life without considering the passé alternatives. More precisely, I'm defining the "base" to be the right hand side of the array and operating on the reversed list -- it's just a change of variables. Python supports -1 based indexing, but does not support 1 based indexing. You could easily define a language which only provides the -1 based indexing but does not support 0-based. Let me demonstrate: void *malloc_r(int size, int count) { void *m = malloc(size, count+1; m[count] = count; return m+count; } void free_r(void *m) { free(m - m[0]); } Memory allocated through malloc_r is right-aligned and you should only use negative indices. Best not touch m[0]!