[Tutor] basic class loading question

Cranky Frankie cranky.frankie at gmail.com
Tue Nov 22 20:25:12 CET 2011

Dave thank you for your patience. It really is appreciated.

In the interest of closing this thread I'm posting the final version
that works, and achieves the goal of showing how to represent data
both in a list and in an object:

# Example to show the difference between list data and object data
class Qb:
    def __init__(self, first_name='', last_name='', phone='',
email='', stadium=''):
        self.first_name = first_name
        self.last_name = last_name
        self.phone = phone
        self.email = email
        self.stadium = stadium

Qb_list = [["Joe", "Montana", "415-123-4567",
"joe.montana at gmail.com","Candlestick Park"],
    ["Fran", "Tarkington","651-321-7657",
"frank.tarkington at gmail.com", "Metropolitan Stadidum"],
    ["Joe", "Namath", "212-222-7777", "joe.namath at gmail.com", "Shea Stadium"],
    ["John", "Elway", "303-9876-333", "john.elway at gmai.com", "Mile
High Stadium"],
    ["Archie", "Manning", "504-888-1234", "archie.manning at gmail.com",
"Louisiana Superdome"],
    ["Roger", "Staubach", "214-765-8989", "roger.staubach at gmail.com",
"Cowboy Stadium"]]

quarterbacks = []                   # Create an empty object list

len_Qb_list = len(Qb_list)          # Get the lenght of the list of
lists which is the
                                    #    number of rows in the input data

for i in range(0, len_Qb_list):     # Iterate for the number of rows

    nq = Qb(*Qb_list[i])            # Create an instance of class Qb called "nq"
                                    #    and populate each field
    quarterbacks.append(nq)         # Append an instance of object
"nq" into object list "quarterbacks"
    i = i + 1                       # Iterate for each row in "Qb_list"

print (quarterbacks[3].phone)       # Print an item from the object
list "quarterbacks"
print (Qb_list[3][2])               # Print the same object from the
original list of lists

Frank L. "Cranky Frankie" Palmeri

More information about the Tutor mailing list