Jul-02-2018, 05:55 AM
Zen of Python violated by Python
According to Zen of Python, Explicit is better than implicit..
However, I thought that the ways to index array using 0 instead of 1, is favoring implicit-ness over explicit-ness.
For example, consider the following code:
In this case, how do you get February? Is it
? NOPE, it is
, because you have to IMPLICITLY adds 1 to the index, since the first item starts from 0, so the second element is at (2-1), which is 1.
Array slicing
Actually, according to Guido (the creator of Python) himself, he thinks that one important thing that helped him to decide to use 0-based or 1-based is slicing.
However, I thought that this is one of the most confusing feature of Python (just to let you know, I can understand in perfectly, but it is just so damn hard to explain to others, which violates another Zen of Python: "If the implementation is hard to explain, it's a bad idea."). Actually, I thinks that's reason why this StackOverflow post have so many upvotes.
So, in my opinion, there is actually a better way for slicing using 1-based index. Consider the following example:
Thus, I have shown you that by using special operators, there is no need to +1 or -1 if we use 1-based index, so, the problem is the operator not the index!
Purpose of this post
This post is not meant to suggest a new feature to Python. However, I just wish to raise out an opinion regarding the violation of Zen of Python by Python itself. Because, Python is meant to be easy to use, but 0-based index is just counter-intuitive (as shown above). Of course, if we live in another world where we naturally count things from zero, using 0-based index would be definitely elegant.
Footnote
I had read Edsger W. Dijkstra, to be honest that is not really convincing, he is just justifying it using his opinion, not facts.
According to Zen of Python, Explicit is better than implicit..
However, I thought that the ways to index array using 0 instead of 1, is favoring implicit-ness over explicit-ness.
For example, consider the following code:
1 |
months = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ] |
1 |
months[ 2 ] |
1 |
months[ 1 ] |
Array slicing
Actually, according to Guido (the creator of Python) himself, he thinks that one important thing that helped him to decide to use 0-based or 1-based is slicing.
However, I thought that this is one of the most confusing feature of Python (just to let you know, I can understand in perfectly, but it is just so damn hard to explain to others, which violates another Zen of Python: "If the implementation is hard to explain, it's a bad idea."). Actually, I thinks that's reason why this StackOverflow post have so many upvotes.
So, in my opinion, there is actually a better way for slicing using 1-based index. Consider the following example:
1 2 3 4 5 6 7 8 9 10 11 |
## Assume the the index starts from one x = [ 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 ] ## let say the pivot is the 5th element pivot = 5 ## How to get the elements before pivot? Using the ..< operator left = x[ 1 ..< pivot] ## How to get from pivot to last? right = x[pivot .. ] |
Purpose of this post
This post is not meant to suggest a new feature to Python. However, I just wish to raise out an opinion regarding the violation of Zen of Python by Python itself. Because, Python is meant to be easy to use, but 0-based index is just counter-intuitive (as shown above). Of course, if we live in another world where we naturally count things from zero, using 0-based index would be definitely elegant.
Footnote
I had read Edsger W. Dijkstra, to be honest that is not really convincing, he is just justifying it using his opinion, not facts.