[Tutor] basic class loading question

Alan Gauld alan.gauld at btinternet.com
Tue Nov 22 20:14:16 CET 2011

On 22/11/11 17:09, Cranky Frankie wrote:

> Dave I'm sorry but I just don't get this. I have virtually no
> experience  with classes.

You need to step back and rethink the terminology a bit.
A class is a cookie cutter for creating objects.
Objects are instances of the class.

> What seems like it should work is this:
> #######################
> len_Qb_list = len(Qb_list)
> for i in range(0, len_Qb_list):
>      quarterbacks = Qb(*Qb_list[i])
>      i = i + 1

This repeatedly creates an instance of Qb and assigns it to the variable 
quarterbacks. quarterbacks always holds the last
instance to be created, the previous instance is destroyed.

You want to create a list (not an instance of Qb but an
ordinary list) which will hold these quarterback objects
you are creating.

> print (quarterbacks[2].last_name)

This tries to print the second something of quarterbacks. But since 
quarterbacks is a single object it fails.

So you need sometjing like this

quarterbacks = []  # an empty list

# get each list entry in turn
for data in Qb_list:
     # create an object and add to the list of quarterbacks

And now

print quarterbacks[2].last_name

makes sense. It will print the last name of the 3rd entry.

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

An instance of Qb is just a quarterback object. You can't load it with 
instances of anything.

You might find it useful to read the OOP topic in my tutorial for a 
different explanation of OOP...


Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list