Re: Exception Names
Tom Anderson wrote:
Is this a good time to mention that in python, iterators don't have a
hasNext method, and instead their next method just throws StopIteration
at the end? :)
Ugh. Out of curiosity, how does a Python programmer manage
multiple "parallel" iterations? For example, let's suppose I
want to print a two-column list of the children in a kindergarten
class, the boys in one column and the girls in the other. In
Java, I might write
List<Child> boys = ...;
List<Child> girls = ...;
Iterator<Child> ib = boys.iterator();
Iterator<Child> ig = girls.iterator();
while (ib.hasNext() || ig.hasNext()) {
if (ib.hasNext())
System.out.print(ib.next());
if (ig.hasNext()) {
System.out.print("\t"); // crude, but just for example
System.out.print(ig.next());
}
System.out.println();
}
Lacking hasNext(), how does the Python programmer proceed? The Java
solution that comes to mind is to write a helper method
static Child getNext(Iterator<Child> it) {
try {
return it.next();
}
catch (NoSuchElementException ex) {
return null;
}
}
.... and use comparisons against null instead of the hasNext() tests.
But this is really just re-implementing hasNext() on the sly! Does
Python offer something better?
--
Eric.Sosman@sun.com