[Python-bugs-list] [ python-Bugs-723806 ] overintelligent slice() behavior on integers
SourceForge.net
noreply@sourceforge.net
Wed, 11 Jun 2003 18:03:37 -0700
Bugs item #723806, was opened at 2003-04-18 11:17
Message generated for change (Settings changed) made by bcannon
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=723806&group_id=5470
Category: Python Interpreter Core
Group: Python 2.2.2
>Status: Closed
Resolution: Wont Fix
Priority: 5
Submitted By: Russ Thompson (russt)
Assigned to: Nobody/Anonymous (nobody)
Summary: overintelligent slice() behavior on integers
Initial Comment:
slices such as a[:7] give a key of slice(0,7,None)
whereas a[:7.1] gives a key of slice(None,7.1,None)
Slices should not assume that the object indices
begin at zero, since that is actually a characteristic of
the object. The current behavior cripples an object that
wants, say, to start its indices with negative numbers.
a[:7] should pass a key of slice(None,7,None).
Thanks!
russt@agilityfund.com
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2003-06-10 21:20
Message:
Logged In: YES
user_id=80475
Russ, you're right about there being two behaviors for the
implementation of the slice notation(see apply_slice() in
Python/ceval.c). This occurs in the bytecode interpreter
rather than some object's implementation of __getitem__.
While it is unfortunate, it can't be fixed without breaking
code which defines __getitem__ but doesn't provide a None-
to-zero converter.
Factiod of the day, interpreter doesn't just assume 0 for
None, it clamps the values in a range of 0<= value <=
sys.maxint:
>>> class C:
def __getitem__(self, i):
return i
>>> c = C()
>>> c[10000000000000000000L:]
slice(2147483647, 2147483647, None)
I recommend you close the bug as Won't Fix.
----------------------------------------------------------------------
Comment By: Russ Thompson (russt)
Date: 2003-06-10 15:23
Message:
Logged In: YES
user_id=760126
'slice' doesn't round its arguments. The difference
is whether you pass an integer or a float as part
of the slice. Thus:
a[:7] gives a key of slice(0,7,None)
while
a[:7.0] gives a key of slice(None,7.0,None)
What's going on I suspect is that the python
has two different behaviours for __getitem__.
The first handles slices with integer only
components, and defaults to starting at zero.
The second handles anything else. This is
probably done in order to accomodate old
'list' and 'tuple' implementation code that doesn't
accept starting indices of None. But the best
solution would be to eliminate the dual behavior
of slice and revise the list and tuple code to
accept the more general behavior.
----------------------------------------------------------------------
Comment By: Brett Cannon (bcannon)
Date: 2003-06-10 14:45
Message:
Logged In: YES
user_id=357491
How does having 'slice' not round its arguments have anything
to do with assuming indices start at zero?
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=723806&group_id=5470