Changes in __getitem__ in Python 2.0?

'Steve' Stephen W. Juranich sjuranic at
Wed Feb 28 07:39:36 CET 2001

It's been a while since I've been playing with my Python toys (I've been
banished to C++ land for a while <retch>).  I have a class with the
following definition for __getitem__:

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.
        retval =[key]
    except IndexError:
        retval =[-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).  I am assuming that the problem is because of the __getitem__
definition above.  Is there a way around this, or am I going to have to
change a bunch of code to make this work on a new Python setup?

I apologize if this is a topic that has been beaten to death.  I tried
looking for somthing relevant on Deja-cum-Google, but I didn't see any
relevant hits and I didn't feel like looking through the next 13,000

Thanks in advance for any help.  I appreciate it.

Stephen W. Juranich                         sjuranic at
Electrical Engineering
University of Washington       

More information about the Python-list mailing list