[Tutor] basic class loading question

Dave Angel d at davea.name
Tue Nov 22 19:48:56 CET 2011


On 11/22/2011 12:09 PM, Cranky Frankie wrote:
> On Tue, Nov 22, 2011 at 11:26 AM, Dave Angel<d at davea.name>  wrote:
> snip
>> quarterbacks = []
>> for ....
>>      quarterbacks.append(       )
>>
>>
>> Now that you really have a list, then you can print a particular one with:
>>
>> print (quarterbacks[2].last_name)
> Dave I'm sorry but I just don't get this. I have virtually no
> experience  with classes.
>
> What seems like it shoud work is this:
>
> #######################
> len_Qb_list = len(Qb_list)
>
> for i in range(0, len_Qb_list):
>      quarterbacks = Qb(*Qb_list[i])
That creates one quarterback, not a list of them.  So you need to append 
that to some list.  As I said in my earlier message, you might want to 
append it to a list called quarterbacks, not replace the earlier object.
>      i = i + 1
>
> print (quarterbacks[2].last_name)
> ############################
>
> In other words, define an instance of the Qb class called
> quarterbacks, and then "load" or instantiate instances of the class
> using the 6 sets of values from Qb_list.
>
> My error message is:
>
> Traceback (most recent call last):
>    File "D:/Python31/q", line 27, in<module>
>      print (quarterbacks[2].last_name)
> TypeError: 'Qb' object does not support indexing
>
>
As long as it's a single object of your class, you can't index it.

Do you have any experience building a list in Python?  If you're trying 
to do it in a for loop. you'd have something like

objects= []     #create empty list
for  .........whatever..........
          newobject = ......something....
          objects.append(newobject)


Now you have a list called objects.   You also have a newobject, which 
is the last one added.


-- 

DaveA



More information about the Tutor mailing list