[Tutor] the chicken and the egg

Kent Johnson kent37 at tds.net
Fri Jan 27 19:14:20 CET 2006


Christopher Spears wrote:
> 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__'

Strange. It works for me. Are you sure there is nothing else in MyList.py?

D:\>type MyList.py
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]


D:\>python
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
  >>> from MyList import *
  >>> m=MyList([0,1,2])
  >>> m[0]
0
  >>> m[1]
1

Kent



More information about the Tutor mailing list