Python Forum
Zen of Python violated by Python
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zen of Python violated by Python
#1
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:

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
In this case, how do you get February? Is it
months[2]
? NOPE, it is
months[1]
, 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:

## 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 .. ]
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.
Reply
#2
Re: indexing - you can read the history, but I don't see how it it's so un-explicit. Where would you say the first centimeter on a ruler starts? Like that, Python uses offsets. Every tutorial should cover this immediately, it's counter-intuitive to many folks but (as Guido explains) the alternative isn't any better.

I couldn't really grok what you were saying about slicing. I didn't follow the SO link. The funky notation seems... funky.

And this might seem wish-washy, but
[quote="The Zen of Python"]Although practicality beats purity.[/python]
Anytime somethings seems contradictory, someone will cite this. If there's a good reason to violate a rule, it'll happen. Arguably, the "assignment" in a for-each loop is implicit. Somehow we have to find a balance.

In any case, I'd have other complaints about Python well before 0-based indexing :)
Reply
#3
If you want to map a number to a string better use a dictionary: {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}.
It's more convenient.
Also, there is a module called calendar. Which has some really useful methods? :)
It's up to you to pick the right data type and format for the job. There is nothing wrong with the zero indexing.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
(Jul-02-2018, 05:55 AM)hou32hou Wrote: 0-based index is just counter-intuitive
In addition to what micseydel already said and also covered in Guido's post - this is subjective (i.e. it is counter-intuitive for you, but not for others (me including)). It is something largely related to previous experience

Real programmers count from 0 :-)
There is plenty of merchandise and jokes related to this - check images

[Image: programmers-cheating-on-counting.jpg]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
0-based indexing does not imply adding 1 to the index. to me 0-based is the natural way to refer to items in a list. 1-based implies subtracting 1.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
I've worked so many times with binary data, chunking it etc.
A 0-based indexing feels more natural.


data = list(range(1, 101, 10)
two_elements = data[:2]
next four_elements = data[2:2+4]
How does this look with 1-based indexing?
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
(Jul-04-2018, 03:05 AM)Skaperen Wrote: to me 0-based is the natural way to refer to items in a list.
ditto. To me 0 based goes along with the zen of python because its the natural method to do it. Most commonly used languages today use 0 based, and it would be an odd ball if it didnt. But that could be just because i have not used 1 based langauges.
Recommended Tutorials:
Reply
#8
I've used both 1-indexed and 0-indexed languages. My feeling is that 1-indexed is more natural if you are not a programmer. If you are a programmer with much experience you are probably used to 0-indexing. Of course, I think a lot of this is confusion about what is being indexed. One based indexing is generally indexing the items in a sequence. Zero based indexing, at least in Python, is indexing the items between the items in the sequence. Once you understand that, things become much clearer.

I fail to see what natrual/intuitive has to do with implicit vs. explicit. You are making the wrong assumption about the language, and saying there has to be an implicit correction to account for it. It's not that the language is being implicit, it's that you made the wrong assumption. The documentation for Python explicitly states that it uses 0-indexing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Whenever I visit USA and get in a lift (elevator) I'm confused because they count the ground floor as floor 1. In the UK, that's ground (0) and the floor above is floor 1, first floor.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#10
(Jul-04-2018, 03:44 PM)gruntfutuk Wrote: Whenever I visit USA and get in a lift (elevator) I'm confused because they count the ground floor as floor 1. In the UK, that's ground (0) and the floor above is floor 1, first floor.

This bit was used (in reverse) in Cryptonomicon as a side joke about 0-indexing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020