473,399 Members | 3,603 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,399 software developers and data experts.

Re: Peek inside iterator (is there a PEP about this?)

On Wed, 01 Oct 2008 10:46:33 -0400, Luis Zarrabeitia wrote:
Hi there.

For most use cases I think about, the iterator protocol is more than
enough. However, on a few cases, I've needed some ugly hacks.

Ex 1:

a = iter([1,2,3,4,5]) # assume you got the iterator from a function and
b = iter([1,2,3]) # these two are just examples.

then,

zip(a,b)

has a different side effect from

zip(b,a)

After the excecution, in the first case, iterator a contains just [5],
on the second, it contains [4,5]. I think the second one is correct (the
5 was never used, after all). I tried to implement my 'own' zip, but
there is no way to know the length of the iterator (obviously), and
there is also no way to 'rewind' a value after calling 'next'.

Ex 2:

Will this iterator yield any value? Like with most iterables, a
construct

if iterator:
# do something

would be a very convenient thing to have, instead of wrapping a 'next'
call on a try...except and consuming the first item.

Ex 3:

if any(iterator):
# do something ... but the first true value was already consumed and
# cannot be reused. "Any" cannot peek inside the iterator without #
consuming the value.

Instead,

i1, i2 = tee(iterator)
if any(i1):
# do something with i2

Question/Proposal:

Has there been any PEP regarding the problem of 'peeking' inside an
iterator?
No (or I'm not aware of any). Why? Because for some iterable, it is not
possible to know in advance its length (video data stream, for example),
or whether it'd ever end (the digits of pi).

Second, in python, iterator is a use-once object, it is not designed to
be reused. Some languages, especially the purely functional ones, allow
multiple use of iterator because they guarantee immutability, python
allows mutable object, and is unable to provide that.
Knowing if the iteration will end or not, and/or accessing the
next value, without consuming it?
No, it is not possible to do that for some iterators. For example, this
code:

import time
class Iterable(object):
def __iter__(self):
return self
def next(self):
return time.time()

if you peeked the iterator in advance, the result would be different
compared to the result when you actually need it.
Is there any (simple, elegant) way
around it?
Simple, but probably not that elegant, if you need such a fine control,
use while loop.
Cheers,

--
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie

Oct 1 '08 #1
0 880

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

Similar topics

19
by: les_ander | last post by:
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to...
1
by: kill sunday | last post by:
I'm working on an RPN calculator, and i can't get the input right. I have to use cin.peek() to check the next character and do whatever i need with it. I can't get it to look for a specific type...
5
by: Mr A | last post by:
Hi! I'm trying to do the following: emplate <typename Resource> class ResourceManager { public: typedef std::list<Resource*>::iterator Iterator; typedef std::list<Resource*>::const_iterator...
9
by: wizofaus | last post by:
Is the any reason according to the standard that calling tellg() on an std::ifstream after a call to peek() could place the filebuf in an inconsistent state? I think it's a bug in the VC7...
4
by: Manfred Braun | last post by:
Hi All ! I think, there is a bug in the System.Console class related to use the STDIO streams. I am doing a very simple thing in a console-based program named CS Console.In.Peek(); and...
3
by: Gerhard Pfeiffer | last post by:
Hi, I'm trying to implement a data-structure and have an iterator for it. Now I've got a problem impleneting the operator+. I tried to isolate the problem: template<int DIM, typename Tclass...
4
by: Rares Vernica | last post by:
Hi, How can I save a reference inside a container? For example I have: map<string, unsignedX; I would like to be able to save a reference to a position inside X. For a vector, the...
5
by: Luis Zarrabeitia | last post by:
Hi there. For most use cases I think about, the iterator protocol is more than enough. However, on a few cases, I've needed some ugly hacks. Ex 1: a = iter() # assume you got the iterator...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
6
by: Pallav singh | last post by:
Hi when we should have Class defined Inside a Class ? can any one give me explanation for it ? Does it is used to Hide some information of Class Data-Member and Function from friend class? ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
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...
0
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,...
0
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...

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.