473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

index out of range with slice object

Hi,

a class of mine should support the list interface and implements the __len__
and __getitem__ methods.

Now when I ask for an unbounded slice:
len( myObj[:] )


my __getitem__(self, y) method gets called with

y = slice(0, 2147483647, None)

Now step size is ok, but where does that incredibly large stop index come
from? Also I expected the start index rather to be None than zero.
The __len__(self) method returns the correct length, which is 728.

Has anybody encountered something like that bevore? Is that natural or am I
doing something wrong?

Thanks in advance
Uwe
Jul 18 '05 #1
2 1740
Uwe Mayer wrote:
a class of mine should support the list interface and implements the
__len__ and __getitem__ methods.

Now when I ask for an unbounded slice:
len( myObj[:] )
my __getitem__(self, y) method gets called with

y = slice(0, 2147483647, None)

Now step size is ok, but where does that incredibly large stop index come
from? Also I expected the start index rather to be None than zero.
The __len__(self) method returns the correct length, which is 728.

Has anybody encountered something like that bevore? Is that natural or am
I doing something wrong?


There seems a transition to be going on, due to extended slices, I suppose:
class Old: .... def __getitem__(self, index):
.... return index
.... class New(object): .... def __getitem__(self, index):
.... return index
.... New()[:] slice(None, None, None) Old()[:] slice(0, 2147483647, None)
I found only the old style behaviour documented in the reference manual
http://www.python.org/doc/current/ref/slicings.html:

"The lower and upper bound expressions, if present, must evaluate to plain
integers; defaults are zero and the sys.maxint, respectively."

So "incredibly large" is in fact sys.maxint, which should be obvious if you
convert it to hexadecimal:
hex(2147483647) '0x7fffffff'

Anyway, you can use the indices() method to get the actual boundaries in
both cases:
Old()[:].indices(10) (0, 10, 1) New()[:].indices(10) (0, 10, 1)


Peter
Jul 18 '05 #2
Peter Otten wrote:
Uwe Mayer wrote:
a class of mine should support the list interface and implements the
__len__ and __getitem__ methods.

Now when I ask for an unbounded slice:
> len( myObj[:] )
my __getitem__(self, y) method gets called with

y = slice(0, 2147483647, None)

Now step size is ok, but where does that incredibly large stop index come
from? Also I expected the start index rather to be None than zero.
The __len__(self) method returns the correct length, which is 728.

Has anybody encountered something like that bevore? Is that natural or am
I doing something wrong?


There seems a transition to be going on, due to extended slices, I
suppose:
class Old: ... def __getitem__(self, index):
... return index
... class New(object): ... def __getitem__(self, index):
... return index
... New()[:] slice(None, None, None) Old()[:] slice(0, 2147483647, None)
I found only the old style behaviour documented in the reference manual
http://www.python.org/doc/current/ref/slicings.html:


Ok, read it again:

"The semantics for an extended slicing are as follows. [...] The conversion
of a proper slice is a slice object (see section 3.2) whose start, stop and
step attributes are the values of the expressions given as lower bound,
upper bound and stride, respectively, substituting None for missing
expressions."

"The lower and upper bound expressions, if present, must evaluate to plain
integers; defaults are zero and the sys.maxint, respectively."

So "incredibly large" is in fact sys.maxint, which should be obvious if
you convert it to hexadecimal:
hex(2147483647) '0x7fffffff'

Anyway, you can use the indices() method to get the actual boundaries in
both cases:
Old()[:].indices(10) (0, 10, 1) New()[:].indices(10) (0, 10, 1)


Peter


Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: kosh | last post by:
I was wondering if there is or there could be some way to pass a generator an optional starting index so that if it supported that slicing could be made more efficient. Right now if you do use a...
45
by: Summercoolness | last post by:
it seems that range() can be really slow: the following program will run, and the last line shows how long it ran for: import time startTime = time.time() a = 1.0
7
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69,...
35
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. ...
16
by: lisa.engblom | last post by:
I have two semi related questions... First, I am trying to output a list of strings to a csv file using the csv module. The output file separates each letter of the string with a comma and then...
85
by: Russ | last post by:
Every Python programmer gets this message occasionally: IndexError: list index out of range The message tells you where the error occurred, but it doesn't tell you what the range and the...
96
by: zzbbaadd | last post by:
What's with the index() function of lists throwing an exception on not found? Let's hope this is rectified in Python 3. If nothing else, add a function that doesn't throw an exception. There are a...
8
by: John Salerno | last post by:
Hey all. I've decided I let my Python skills (minor though they were) slip away so I started reading the new edition of Learning Python to brush up. I just read about lists again and I'm wondering...
3
by: jmDesktop | last post by:
This program: s = 'abcde' i = -1 for i in range (-1, -len(s), -1): print s, i gives abcd -1
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.