473,788 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sequence iterators with __index__

I think it would be useful if iterators on sequences had the __index__
method so that they could be used to slice sequences. I was writing a
class and wanted to return a list iterator to callers. I then wanted
to let callers slice from an iterator's position, but that isn't
supported without creating a custom iterator class.

Are there reasons for not supporting this generally? I realize not all
iterators would have the __index__ method, but that seems ok.

In Python 3, maybe this could be called a SequenceIterato r

-Brad
Jun 27 '08 #1
7 2441
On Jun 24, 3:29*pm, schickb <schi...@gmail. comwrote:
I think it would be useful if iterators on sequences had the __index__
method so that they could be used to slice sequences. I was writing a
class and wanted to return a list iterator to callers. *I then wanted
to let callers slice from an iterator's position, but that isn't
supported without creating a custom iterator class.

Are there reasons for not supporting this generally? I realize not all
iterators would have the __index__ method, but that seems ok.

In Python 3, maybe this could be called a SequenceIterato r

-Brad
Could you post an example of what you are talking about? I'm not
getting it. In any case, the first step is writing a PEP.
http://www.python.org/dev/peps/

Matt
Jun 27 '08 #2
On Jun 24, 3:45*pm, Matimus <mccre...@gmail .comwrote:
>
I think it would be useful if iterators on sequences had the __index__
method so that they could be used to slice sequences. I was writing a
class and wanted to return a list iterator to callers. *I then wanted
to let callers slice from an iterator's position, but that isn't
supported without creating a custom iterator class.

Could you post an example of what you are talking about? I'm not
getting it.
Interactive mock-up:
>>a = ['x','y','z']
it = iter(a)
a[it:]
['x', 'y', 'z']
>>it.next()
'x'
>>a[it:]
['y', 'z']
>>a[:it]
['x']
>>it.next()
'y'
>>a[it:]
['z']

This lets you use sequence iterators more general position indicators.
Currently if you want to track a position and slice from a tracked
position you must do it manually with an integer index. It's not
difficult, but given that sequence iterators already do that already
it seems redundant (and of course more error prone).

In any case, the first step is writing a PEP.http://www.python.org/dev/peps/
Ok thanks, but I do want some idea of interest level before spending a
bunch of time on this.

-Brad
Jun 27 '08 #3


schickb wrote:
On Jun 24, 3:45 pm, Matimus <mccre...@gmail .comwrote:
>>I think it would be useful if iterators on sequences had the __index__
method so that they could be used to slice sequences. I was writing a
class and wanted to return a list iterator to callers. I then wanted
to let callers slice from an iterator's position, but that isn't
supported without creating a custom iterator class.
Creating custom classes is what the class statement is for. See below.
>Could you post an example of what you are talking about? I'm not
getting it.

Interactive mock-up:
>>>a = ['x','y','z']
it = iter(a)
a[it:]
['x', 'y', 'z']
>>>it.next()
'x'
>>>a[it:]
['y', 'z']
>>>a[:it]
['x']
>>>it.next()
'y'
>>>a[it:]
['z']

This lets you use sequence iterators more general position indicators.
Currently if you want to track a position and slice from a tracked
position you must do it manually with an integer index. It's not
difficult, but given that sequence iterators already do that already
it seems redundant (and of course more error prone).
Python's iterator protocol is intentionally simple and general.
Wanting to slice while iterating is a *very* specialized usage.
In any case:
A. If the iterator uses in incrementing index to iterate, you want access.
B. Using an iterator as an integer will strike most people as
conceptually bizarre; it will never be accepted.
C. Doing so is unnecessary since the internal index can just as easily
be exposed as an integer attribute called 'index' or, more generally,
'count'.
a[it.count:] looks *much* better.
D. You can easily add .index or .count to any iterator you write. The
iterator protocol is a minimum rather than maximum specification.
E. You can easily wrap any iterable/iterator in an iterator class that
provides .count for *any* iteration process. Slicing is not the only
possible use of such an attribute.

class indexerator():
def __inti__(self, iterable):
self.it = iter(iterable)
self.count = 0
def __iter__(self): return self
def __next__(self): # 3.0
tem = self.it.next()
self.count += 1
return tem

..count is always the number of items returned. It is also the index of
the next item to be returned if (but only if() the base iterable exists
and is an indexable sequence.
F. Even this should be unnecessary for most usages. Built-in function
enumerate(itera ble) generates count,item pairs in much the same manner:
>In any case, the first step is writing a PEP.http://www.python.org/dev/peps/
Discussion usually comes first.
Ok thanks, but I do want some idea of interest level before spending a
bunch of time on this.
Even the developers sometimes take this precaution. Many ideas never
make it to the PEP stage. I see no need for a change in Python here.

Terry Jan Reedy

Jun 27 '08 #4
On Jun 24, 5:46 pm, Terry Reedy <tjre...@udel.e duwrote:
>
Wanting to slice while iterating is a *very* specialized usage.
I disagree because iterators mark positions, which for sequences are
just offsets. And slicing is all about offsets. Here is a quote from
the already implemented PEP 357:

"Currently integers and long integers play a special role in slicing
in that they are the only objects allowed in slice syntax. In other
words, if X is an object implementing the sequence protocol, then
X[obj1:obj2] is only valid if obj1 and obj2 are both integers or long
integers. There is no way for obj1 and obj2 to tell Python that they
could be reasonably used as indexes into a sequence. This is an
unnecessary limitation."

But this isn't just about slicing. I'd like sequence iterators to be
usable as simple indexes as well; like a[it] (which __index__ would
also provide).
In any case:
A. If the iterator uses in incrementing index to iterate, you want access.
B. Using an iterator as an integer will strike most people as
conceptually bizarre; it will never be accepted.
It's not meant to be used as an integer. It's meant to be used as a
position in the sequence, which iterators already are. The fact that
the position is represented as an integer is not that important
(except to python). I'll grant you that it is conceptually strange
that you could use an iterator on one sequence as an index into
another.
C. Doing so is unnecessary since the internal index can just as easily
be exposed as an integer attribute called 'index' or, more generally,
'count'.
a[it.count:] looks *much* better.
D. You can easily add .index or .count to any iterator you write. The
iterator protocol is a minimum rather than maximum specification.
Following that line of reasoning, the __index__ special method
shouldn't really exist at all. Your arguments would suggest that NumPy
shouldn't use __index__ either because:
a[ushort.index] "looks *much* better".
E. You can easily wrap any iterable/iterator in an iterator class that
provides .count for *any* iteration process.
Sure, and that is why I mentioned this in my original post. But the
idea is to avoid redundant code and data in the case of sequences, and
make it a standard feature.
F. Even this should be unnecessary for most usages. Built-in function
enumerate(itera ble) generates count,item pairs in much the same manner:
I am not aware of a way to get the current position out of an
enumerate object without advancing it (or creating a custom wrapper).
If the special __index__ method was added it might be interesting ;)
But iterators are already a clean and abstract position marker, and
for sequences it seems surprising to me that they can't really be used
as such.

-Brad
Jun 27 '08 #5
On Jun 25, 12:11*am, schickb <schi...@gmail. comwrote:
>
But this isn't just about slicing. I'd like sequence iterators to be
usable as simple indexes as well; like a[it] (which __index__ would
also provide).
It occurred to me that this wouldn't need to be limited to sequence
iterators. Although somewhat of a misnomer, __index__ could just a
well return the current key for mapping iterators. Type checking would
then be specific to the context, rather than hard-coded to integer.
Perhaps __position__ would have been a better name.

The generalized idea here is that iterators identify positions in
collections, so why shouldn't they be usable where collections accept
such identifiers?
>>m = {'a':1, 'b':2}
it = iter(m)
m[it]
1
>>it.next()
m[it]
2

These are trivial examples, but there are lots of uses for abstract
position indicators. From what I've seen, iterators are currently used
almost exclusively as temporary objects in loops. But perhaps if they
had a bit more functionality they could serve a wider purpose.

-Brad
Jun 27 '08 #6
On Jun 24, 4:19*pm, schickb <schi...@gmail. comwrote:
On Jun 24, 3:45*pm, Matimus <mccre...@gmail .comwrote:
I think it would be useful if iterators on sequences had the __index__
method so that they could be used to slice sequences. I was writing a
class and wanted to return a list iterator to callers. *I then wanted
to let callers slice from an iterator's position, but that isn't
supported without creating a custom iterator class.
Could you post an example of what you are talking about? I'm not
getting it.

Interactive mock-up:
>a = ['x','y','z']
it = iter(a)
a[it:]
['x', 'y', 'z']
>it.next()
'x'
>a[it:]
['y', 'z']
>a[:it]
['x']
>it.next()
'y'
>a[it:]

['z']

This lets you use sequence iterators more general position indicators.
Currently if you want to track a position and slice from a tracked
position you must do it manually with an integer index. It's not
difficult, but given that sequence iterators already do that already
it seems redundant (and of course more error prone).
In any case, the first step is writing a PEP.http://www.python.org/dev/peps/

Ok thanks, but I do want some idea of interest level before spending a
bunch of time on this.

-Brad
I have no problem with being able to query the position (state) of an
iterator without changing its state. I think using the iterator itself
as the index or part of a slice in the original sequence is non-
obvious and also less useful than just a new method to query state.
"Explicit is better than Implicit". I would rather see just
`it.index()` or `it.state()` than the new specialized behavior implied
by `it.__index__() `. I'm leaning towards `state` because sequences
already have an `index` method, and two methods of the same name with
different behaviors may be confusing. This gives you essentially the
same ability, and the code seems a little more obvious IMHO.
>>a = ['x','y','z']
it = iter(a)
a[it.state():]
['x', 'y', 'z']
>>it.next()
'x'
>>a[it.state():]
['y', 'z']
>>a[:it.state()]
['x']
>>it.next()
'y'
>>a[it.state():]
['z']
>>it.state()
2
Matt
Jun 27 '08 #7
On Jun 24, 4:19 pm, schickb <schi...@gmail. comwrote:
On Jun 24, 3:45 pm, Matimus <mccre...@gmail .comwrote:
I think it would be useful if iterators on sequences had the __index__
method so that they could be used to slice sequences. I was writing a
class and wanted to return a list iterator to callers. I then wanted
to let callers slice from an iterator's position, but that isn't
supported without creating a custom iterator class.
Could you post an example of what you are talking about? I'm not
getting it.

Interactive mock-up:
>a = ['x','y','z']
it = iter(a)
a[it:]
['x', 'y', 'z']
>it.next()
'x'
>a[it:]
['y', 'z']
>a[:it]
['x']
>it.next()
'y'
>a[it:]

['z']

This lets you use sequence iterators more general position indicators.
Currently if you want to track a position and slice from a tracked
position you must do it manually with an integer index. It's not
difficult, but given that sequence iterators already do that already
it seems redundant (and of course more error prone).
In any case, the first step is writing a PEP.http://www.python.org/dev/peps/

Ok thanks, but I do want some idea of interest level before spending a
bunch of time on this.

-Brad

Brad,

enumerate() seems to solve this problem for me....
>>
a = ['x','y','z']
ea = enumerate(a)
index, value = ea.next()
index
0
>value
'x'
>index, value = ea.next()
a[index:]
['y', 'z']
>>
putting this bit of code in a thin class wrapper should be useful to
keep the two data objects in sync. adding a reset() method allows you
to rebuild the enumerate object as often as needed.

Hope this helps.

--Alan
Jun 27 '08 #8

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

Similar topics

10
1815
by: David Murmann | last post by:
Hi all! I could not find out whether this has been proposed before (there are too many discussion on join as a sequence method with different semantics). So, i propose a generalized .join method on all sequences with these semantics: def join(self, seq): T = type(self) result = T()
5
2234
by: sandravandale | last post by:
I want to be able to pass a sequence (tuple, or list) of objects to a function, or only one. It's easy enough to do: isinstance(var, (tuple, list)) But I would also like to accept generators. How can I do this? Anything else is assumed to be a single value (my fault if I pass a
2
2355
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
11
3702
by: Wilfried Mestdagh | last post by:
Hi, does foreach guarantees the sequence order starting from index 0 ? -- rgds, Wilfried http://www.mestdagh.biz
10
4168
by: rplobue | last post by:
im trying to get urllib2 to work on my server which runs python 2.2.1. When i run the following code: import urllib2 for line in urllib2.urlopen('www.google.com'): print line i will always get the error:
7
2593
by: desktop | last post by:
In the C++ standard page 472 it says that you can construct a std::set in linear time if the constructor gets a sorted sequence of elements. But how is this possible when insert takes logarithmic time? Should the time not be nlgn where n is the number of elements?
1
3693
by: Anan18 | last post by:
Hi there, I revised my sequence class, from the last homework to use a dynamic array to store items, i am getting an error and 4 warnings, i also provided my header file, as well as the implementation file, can someone please help me out hear, my brains are beyond fried now, highly appreciated 1>------ Build started: Project: Assignment3, Configuration: Debug Win32 ------ 1>Compiling... 1>sequence2.cxx...
21
1610
by: bilgekhan | last post by:
After doing a succcessful insert() or find() on a set<Tcontainer is it possible to get the item number of this item in the set? (ie. its zero-based sequence number (position/location/rank/index) within the sorted collection) Additional info: insert() returns a pair<Tand find() returns an iterator. Using any of these two objects is it possible to get the sequence number of the item these objects point to? After the insert() or find()...
2
1673
by: jimxoch | last post by:
Dear list, I have recently implemented a generic sequence searching template function, named single_pass_search, which is more generic than std::search and has better worst case complexity at the same time. More specifically, the main advantage of the single_pass_search over the std::search is its capability to search for a sub-sequence in a search-range that is accessed through a pair of single-pass (input) iterators, while...
0
10366
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, 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...
0
10173
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 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...
1
10110
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,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
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...
0
6750
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();...
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.