iteration (was RE: "sins" (aka, acknowledged language problems))
bjorn
bjorn at roguewave.com
Fri Dec 17 12:00:06 EST 1999
Aahz Maruch wrote:
> In article <6D8A17398E28D3119F860090274DD7DB4B3D53 at pces.cadlab.it>,
> Alex Martelli <Alex.Martelli at think3.com> wrote:
> >
> >but what should I place instead of the 'pass' statements, to make the
> >'for' construct terminate correctly...? In other words, what does
> >__getitem__ return, or what exception does it raise, to make a "for
> >x in y" statement terminate correctly? I see from the sources for
> >fileinput.py that an IndexError gets raised -- is that the "right" way
> >to do it (is it documented somewhere as such), or does it just "happen
> >to work" on the current implementation of the interpreter...?
>
> Yes, the "for" construct contains an implicit try/except on IndexError
> that it uses for loop control. It is documented somewhere, but I don't
> off-hand know where -- this is not likely to change, though it may get
> extended.
And to make sure we can get off on another tangent... ;-)
I recently had to implement my own special purpose list-like structure in c++ for
use by my python program. I implemented __getitem__ and __len__ and was naive
enough to think that that would be enough for:
for x in myList:
foo(x)
but of course it caused a coredump when python kept requesting items way beyond
the __len__ of the container... I solved it by:
i = 0
while i < len(myList):
x = myList[i]
foo(x)
which works, but wasn't what I was hoping for (*sigh*)... Anyone know how to
raise an IndexError through SWIG?
-- bjorn
More information about the Python-list
mailing list