Changes in __getitem__ in Python 2.0?

Fredrik Lundh fredrik at pythonware.com
Wed Feb 28 02:14:14 EST 2001


"'Steve' Stephen W. Juranich" wrote:
> def __getitem__(self, key):
>     # In some cases, I'm getting requests for data that are
>     # past the end of the list.  Given the sloppy nature of
>     # the hand-labelled data, I don't think this is too much
>     # of a problem, so I'll try this scheme.
>     try:
>         retval = self.data[key]
>     except IndexError:
>         retval = self.data[-1]
>
>     return retval
>
> I'm now encountering a problem with Python 2.0 that I never had with earlier
> versions (1.5 and 1.6 specifically).
>
> I used to be able to do something like this:
>
> for foo in MyClass:
>    print foo.member,
>
> But now when I do this, Python just starts spinning out of control in an
> infinite loop (it's like a car wreck... I want to press ^C, but I just can't
> look away).

it spins out of control in 1.5.2 too: for-in expects getitem to
throw an index exception when there are no more items.

afaik, for-in has always worked that way.

the real problem is probably somewhere else...

Cheers /F





More information about the Python-list mailing list