Changes in __getitem__ in Python 2.0?

'Steve' Stephen W. Juranich sjuranic at u.washington.edu
Wed Feb 28 01:39:36 EST 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.
    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).  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
possibilities.

Thanks in advance for any help.  I appreciate it.

----------------------------------------------------------------------
Stephen W. Juranich                         sjuranic at ee.washington.edu
Electrical Engineering         http://students.washington.edu/sjuranic
University of Washington                 http://ssli.ee.washington.edu





More information about the Python-list mailing list