[Tutor] basic class loading question
Dave Angel
d at davea.name
Tue Nov 22 14:39:22 CET 2011
On 11/22/2011 08:17 AM, Cranky Frankie wrote:
> I have a basic question about how to load a class. If I have this class:
>
> 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
>
> and this data:
>
> 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"]]
>
> What is the best way to load it? I'm thinking there should be an
> append method, but I'm having trouble getting it to work.
>
>
No idea what you mean by "load a class."
You can create an object by the classname and parentheses, containing
the paramters as specified in the __init__() method. So since your
class was called QB (which should be Qb, by naming conventions):
obj = QB("Joe", "Montana", "415-213-4567",
joe.montana at gmail.com","Candlestick Park")
and since that happens to be one element of your list, you could use
obj = QB( *QB_list[0] )
If you want a collection of such objects, you might want to write a loop, and then indeed the append method might be useful.
Write some code, and show us where you get stuck.
--
DaveA
More information about the Tutor
mailing list