[Tutor] the chicken and the egg
Christopher Spears
cspears2002 at yahoo.com
Fri Jan 27 18:12:58 CET 2006
Thanks to all of the tutors on this mailing list! I'm
finally making some headway! I originally decided to
tackle my problem one operator at a time:
class MyList:
def __init__(self, aList=None):
if aList is None:
self.mylist = []
else:
self.mylist = aList[:]
def __getitem__(self, index):
return self.mylist[index]
However, I got the following error:
>>> from MyList import *
>>> x = MyList([1,2,3])
>>> x[0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "MyList.py", line 8, in __getitem__
self.mylist[index]
AttributeError: MyList instance has no attribute
'__setitem__'
>>> x.mylist[0]
1
I did the obvious and created __setitem__.
class MyList:
def __init__(self, aList=None):
if aList is None:
self.mylist = []
else:
self.mylist = aList[:]
def __getitem__(self, index):
return self.mylist[index]
def __setitem__(self, index, value):
self.mylist[index] = value
Why does __getitem require __setitem__? Don't they do
different things?
More information about the Tutor
mailing list