472,342 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Using and Implementing iterators with classes such as linked lists

I am taking a second programming course in Java and am currently
attempting to apply what I have learned using Python instead. One
thing that is puzzling me is how to use an iterator.

I am writing a module containing everything I'd need for canonical
linked lists. One particularly useful feature would be to use a for
loop over the whole structure. For this I have learned the benefits
of
iterators. I have read a few book entries on Python iterators, as
well
as an online article by David Mertz, I believe, and PEP 234, and I
may
be lacking some insight but I am still confused about one thing. How
does the iteration know where to begin?

AFAIU, in my LinkedList class, I can either have the LinkedList be
its
own iterator by making its __iter__() method return self and defining
a next() method, or I can have a separate iterator called from
LinkedList's __iter__(). My view is that it would be best to have the
LinkedList be its own iterator - is that the case? Or is an external
iterator preferable in this case?

My problem with implementing the former comes with this: in
LinkedList
I would have:

....

def __init__(self):
return self

....

def next(self):
if self.__current.next == None:
raise StopIteration
self.__current = self.__current.next
return self.__current.next

....

Now, is this good in the eyes of more experienced programmers? Also,
do I really want to dedicate an instance variable to keep track of
where to begin iteration (if __current is used for other purposes,
iteration could conceivably begin anywhere right?)? Does this suggest
that I should have a separate LinkedListIterator class? And if I do
have that separate class, do I make the LinkedList.__iter__() pass on
to LinkedListIterator's constructor the head node of LinkedList, or
the whole linked list?

Thanks in advance,

Jeremy

__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

Jul 18 '05 #1
3 5525
Adelein and Jeremy <ad**************@yahoo.com> wrote in
news:ma*************************************@pytho n.org:
AFAIU, in my LinkedList class, I can either have the LinkedList be
its
own iterator by making its __iter__() method return self and defining
a next() method, or I can have a separate iterator called from
LinkedList's __iter__(). My view is that it would be best to have the
LinkedList be its own iterator - is that the case? Or is an external
iterator preferable in this case?


It sounds a very bad idea to have LinkedList be its own iterator. If you
did that then you could only have one iteration over each list at a time.
In practical use, you will find that you want to iterate over a list, and
you don't know or care whether another piece of code is already iterating
over the same list.

The easiest way to handle it here is to use a generator to do the actual
iteration:

def __iter__(self):
def iterate(current):
while current.next is not None:
next = current.next
yield current
current = next
return iterate(self)
--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #2
In article <Xn**************************@127.0.0.1>,
Duncan Booth <du****@rcp.co.uk> wrote:

def __iter__(self):
def iterate(current):
while current.next is not None:
next = current.next
yield current
current = next
return iterate(self)


Huh? Why the extra function and complexity?

def __iter__(self):
current = self
while current is not None:
yield current
current = current.next
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.
Jul 18 '05 #3
aa**@pythoncraft.com (Aahz) wrote in news:bq**********@panix3.panix.com:
In article <Xn**************************@127.0.0.1>,
Duncan Booth <du****@rcp.co.uk> wrote:

def __iter__(self):
def iterate(current):
while current.next is not None:
next = current.next
yield current
current = next
return iterate(self)


Huh? Why the extra function and complexity?

def __iter__(self):
current = self
while current is not None:
yield current
current = current.next


Because I was asleep.

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #4

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

Similar topics

1
by: adeleinandjeremy | last post by:
I am taking a second programming course in Java and am currently attempting to apply what I have learned using Python instead. One thing that is...
7
by: sks_cpp | last post by:
Consider the following: list<int> a; // assume a contains some number of integers list<int>::iterator iter = a.find(10); for (iter; iter <...
0
by: D.C.Dunn | last post by:
Please forgive the naivety of my following question. I'm new to C++, and am trying to get to grips with OOP. My question concerns iterators and...
3
by: s_subbarayan | last post by:
Dear all, 1)In one of our implementation for an application we are supposed to collate two linked lists.The actual problem is like this: There...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial...
1
by: sk.rasheedfarhan | last post by:
Hi , I am using C# I am having 4 classes. like below. public class A { String m_strRuleName; String m_strRuleGuid; // Some member functions....
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class...
9
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.