473,659 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

discarded iterator.next() at interactive global scope doesn't bump interator??

Is this a well known bug that's been fixed?
I couldn't find any discussion of it, but maybe my googling's off today ;-/
def foo(): ... it = iter(range(10))
... while True:
... i = it.next()
... print i
... if i&3==0:
... print 'skipping next'
... it.next()
...
------------------< This looks ok: foo() 0
skipping next
2
3
4
skipping next
6
7
8
skipping next
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in foo
StopIteration ------------< now at interactive global scope: it = iter(range(10))
while True: ... i = it.next()
... print i
... if i&3==0:
... print 'skipping next'
... it.next()
...
-----<this doesn't look ok: it.next() is apparently not doing anything:
0
skipping next
1
2
3
4
skipping next
5
6
7
8
skipping next
9
Traceback (most recent call last):
File "<stdin>", line 2, in ?
StopIteration

----------------------------< but add a print, and all is well?? it = iter(range(10))
while True:

... i = it.next()
... print i
... if i&3==0:
... print 'skipping next'
... print '-->', it.next()
...
0
skipping next
--> 1
2
3
4
skipping next
--> 5
6
7
8
skipping next
--> 9
Traceback (most recent call last):
File "<stdin>", line 2, in ?
StopIteration

I guess it could be in the read-eval-print loop

I have an old 2.4 on NT4 and am running it in a console window.
Python 2.4b1 (#56, Nov 3 2004, 01:47:27)
[GCC 3.2.3 (mingw special 20030504-1)] on win32
Type "help", "copyright" , "credits" or "license" for more information.

Regards,
Bengt Richter
Sep 4 '05 #1
2 1502
Bengt Richter wrote:

[it.next() appears to be a noop in the interactive interpreter]
I guess it could be in the read-eval-print loop


Indeed:
for i in range(5): .... 42
....
42
42
42
42
42

Whereas:
for i in range(5): .... None


Every line with an expression that doesn't evaluate to None is echoed.
Therefore

it.next()

and

print it.next()

look the same when repr(it.next()) == str(it.next()).

No bug :-)

Peter

Sep 4 '05 #2
On Sun, 04 Sep 2005 10:10:33 +0200, Peter Otten <__*******@web. de> wrote:
Bengt Richter wrote:

[it.next() appears to be a noop in the interactive interpreter]
I guess it could be in the read-eval-print loop


Indeed:
for i in range(5):... 42
...
42
42
42
42
42

Whereas:
for i in range(5):... None


Every line with an expression that doesn't evaluate to None is echoed.
Therefore

it.next()

and

print it.next()

look the same when repr(it.next()) == str(it.next()).

No bug :-)

D'oh. I said it, but I didn't hear myself ;-/

Regards,
Bengt Richter
Sep 4 '05 #3

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

Similar topics

38
3661
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
5
1686
by: Hunter Hou | last post by:
Hi, I just ran into one simple but confusing problem. Here is my code skeleton: int main() { vector<int> vecInt( 5 ); typedef vector<int>::interator VI for( VI p = vecInt.begin(); p != vecInt.end(); p++ ) { .......
3
2277
by: uclamathguy | last post by:
I am working on connected component analysis, but that is irrelevant. I have a mapping containing ints as the keys and sets of ints as the "values." Given an integer, I need to iterate through the map, which means I must iterate through all of the sets in the map. I need an iterator that points to the set where the integer was found. How can I do this? What will the type of this iterator be? Can you help me with a code snippet?
6
6652
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
14
4867
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments to an iterator method (one returning IEnumerable). I WOULD like to do this for transformative operations on a collection. I realize the need to lock target, etc. Does anyone know how to handle 'writable iterators' in C# 2?
27
5307
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an iterator. I have: class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self._next()
2
3808
by: kjackson.ken | last post by:
I have code such as the following: map<int, stringstringMap; stringMap.insert(map<int,string>::value_type(1, "hello")); map<int, string>::iterator iter; iter = stringMap.find(1);
12
4521
by: xgngli | last post by:
Suppose we have a vector: vector<intvec(10); We can declare a iterator this way: vector<int>::iterator vecItor; and then dereference it like this: for (vecItor = vec.begin(); vecItor != vec.end(); vecItor++) { cout << *vecItor << endl;
5
2066
by: Kenneth Porter | last post by:
Is there a stylistically preferred way to apply an algorithm to a vector of vectors? For example, suppose I have: typedef vector< vector<int Array2D; Array2D array2D; I want to find the top-level element who's 2nd element is the maximum (think searching on column 2). The obvious algorithm is max_element, but do
0
8851
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
8751
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
8535
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
8629
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 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...
0
7360
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...
0
5650
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();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2757
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
1739
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.