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__(sel f, 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 2 1766
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__(sel f, 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__(sel f, index):
.... return index
.... class New(object):
.... def __getitem__(sel f, 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
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__(sel f, 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__(sel f, index): ... return index ... class New(object): ... def __getitem__(sel f, 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 generator and do a or any
other kind of slice it reads all the values up to 100 also.
I know a lot of generators have to do all previous parts before they can do
the next part but it would be a nice optimization that can start at any index
to...
|
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
|
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, Apr 11 2006, 15:32:42)
on win32
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
|
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.
import shutil, os, sys
|
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 puts each string on a separate line. So the code is:
import csv
output = csv.writer(open('/Python25/working/output.csv', 'a'))
a =
for elem in range(len(a)):
| |
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 offending index are. Why does it force you to
determine that information for yourself when it could save you a step
and just tell you? This seems like a "no-brainer" to me. Am I missing
something?
|
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 million
situations where you can have an item not be in a list and it is not
an exception situation.
|
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 if someone
could explain what's going on under the hood that makes index and slice
assignments behave differently when assigning an empty list.
For example:
|
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
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |