I was playing around with sliceobject.c this evening and noticed the following behavior. If you slice with a step 0, you receive a ValueError but when you slice with a step of None, the step is set to 1. As an example, observe the following interactive session:
a = [1,2,3,4,5,6] b = slice(0,5,None) a[b] [1, 2, 3, 4, 5] b = slice(0,5,0) a[b] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: slice step cannot be zero
Within the code, it looks like Py_None performs a step of 1. Does it make sense to create a patch so that None and 0 behave the same in this respect?
a = [1,2,3,4,5,6] b = slice(0,5,None) a[b] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: slice step cannot be None b = slice(0,5,0) a[b] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: slice step cannot be zero b = slice(0,5) a[b] [1, 2, 3, 4, 5]
Joe
I think that should not change. None is different than 0. It makes sense to use it as a "use the default value" kind of place holder. Silently using 1 when you pass 0 is a very different thing. Maybe the slice was calculated and the developer should know about it being 0, because in this case they really don't want a step of 1, or the calculation was broken. There are lots of reasons. On Dec 11, 2007, at 9:23 PM, Joseph Armbruster wrote:
I was playing around with sliceobject.c this evening and noticed the following behavior. If you slice with a step 0, you receive a ValueError but when you slice with a step of None, the step is set to 1. As an example, observe the following interactive session:
a = [1,2,3,4,5,6] b = slice(0,5,None) a[b] [1, 2, 3, 4, 5] b = slice(0,5,0) a[b] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: slice step cannot be zero
Within the code, it looks like Py_None performs a step of 1. Does it make sense to create a patch so that None and 0 behave the same in this respect?
a = [1,2,3,4,5,6] b = slice(0,5,None) a[b] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: slice step cannot be None b = slice(0,5,0) a[b] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: slice step cannot be zero b = slice(0,5) a[b] [1, 2, 3, 4, 5]
Joe _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/ ironfroggy%40socialserve.com
participants (2)
-
Calvin Spealman
-
Joseph Armbruster