__getitem__ AttributeError

Alex Martelli aleaxit at yahoo.com
Mon Oct 30 09:44:33 EST 2000


"Jeroen Valcke" <jeroen.valcke at savaco.com> wrote in message
news:39FD86B3.E13ABF1E at savaco.com...
> In python I call a method of a selfdeclared class and I keep getting the
> error:
>     H:\progs\python>python listslides.py
>     Traceback (innermost last):
>       File "listslides.py", line 18, in ?
>         for slide in slidelist:
>     AttributeError: __getitem__
>
> What am I doing wrong?

You are trying to iterate over a slidelist.Slidelist instance
in a for statement, at line 18 in listslides.py:

> for slide in slidelist:

and class Slidelist does not define a __getitem__ method,
so its instances cannot be indexed with [] nor iterated on
with for.


If I understand your intentions correctly, the __getitem__
method you may need in the Slidelist class may be one that
just delegates to the self.slidelist attribute of the
instance (you _are_ naming *quite a few* disparate things
"slidelist", here, aren't you...?), i.e.:

class Slidelist:
    def __init__(self):
        self.slidelist = []
    def __getitem__(self, index):
        return self.slidelist[index]
    # etc, rest of class snipped


Alex






More information about the Python-list mailing list