Newbie: List of instances?

Jeff Layton laytonjb at bellsouth.net
Fri Mar 29 15:55:34 EST 2002


Peter Hansen wrote:

> 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.)

I thought this was something simple, so I didn't post any code
(it's really throw away code anyway). In the future I'll post all
of the code and the tracebacks.


>
>
> 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

You're correct. Somehow the paste didn't work. Sorry about
that.


>
>
> So always cut-and-paste errors into emails...
>
> -Peter





More information about the Python-list mailing list