Newbie: List of instances?
Peter Hansen
peter at engcorp.com
Fri Mar 29 15:10:15 EST 2002
Jeff Layton wrote:
>
> a = []
> a[0] = Bob( 'localhost' )
> IndexError: list assignment index out of range
Please always cut and paste the actual traceback into the message
so we can see what the interpreter really says. (In this case
it's okay though.)
You can't assign to the first entry because the list is empty.
You need to append to the list, to let it grow as needed,
sort of like you tried here:
> I also tried it this way,
>
> a = []
> a.append = Bob('localhost')
That won't work, because append is a method so you have to pass
the argument in brackets:
a.append(Bob('localhost'))
> and I get the error message:
>
> a[0] = Bob( 'localhost' )
> IndexError: list assignment index out of range
That's not the error message you would get from the a.append
attempt you did. That would have said this:
>>> a.append = Bob('localhost')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'list' object attribute 'append' is read-only
So always cut-and-paste errors into emails...
-Peter
More information about the Python-list
mailing list