Re: [Tutor] basic class loading question
Cranky Frankie wrote: OK, but this is still not working: 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.mont...@gmail.com,Candlestick Park], [Fran, Tarkington,651-321-7657, frank.tarking...@gmail.com, Metropolitan Stadidum], [Joe, Namath, 212-222-, joe.nam...@gmail.com, Shea [Stadium], John, Elway, 303-9876-333, john.el...@gmai.com, [Mile High Stadium], [Archie, Manning, 504-888-1234, archie.mann...@gmail.com, Louisiana Superdome], [Roger, Staubach, 214-765-8989, roger.staub...@gmail.com, Cowboy Stadium]] len_Qb_list = len(Qb_list) for i in range(0, len_Qb_list): quarterbacks = Qb(*Qb_list[i]) i = i + 1 print(quarterbacks.last_name(2)) Hi, this is my first post in this group so hopefully I don't mess up. I think your real problem here is the for-loop and not the class instantiation itself. Below is the minimally modified working version of your code: len_Qb_list = len(Qb_list) for i in range(0, len_Qb_list): quarterbacks = Qb(*Qb_list[i]) print(quarterbacks.last_name) Have a look at the differences: - The for-loop itself increments i, you don't have to do it yourself - The printed expression is different: quarterbacks.last_name(2) implies a function call, whereas quarterbacks.last_name refers to an attribute - The indentation was modified by me to print every last name. With your indentation only the last last name would be printed. Since I'm not sure what you want to accomplish this may be wrong for you. Here is a somewhat nicer version, which does exactly the same: quarterbacks = [Qb(*quarterback) for quarterback in Qb_list] for quarterback in quarterbacks: print(quarterback.last_name) It is easier to read (at least in my opinion) and less typing too. If you'd like to understand the syntax, i'd recommend you to look up list comprehension in your book of choice. This would be the minimal version of code to print out all the last names(at least at my level of understanding): [print(Qb(*quarterback).last_name) for quarterback in Qb_list] I hope this helps. I'm new to python, so please take my advice with a grain of salt. Cheers ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] basic class loading question
On 11/22/2011 11:25 AM Cranky Frankie said... snip quarterbacks = [] # Create an empty object list In the interest of preferred techniques, your loop will be more pythonic when you write this part... 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 ...like this... for this_qb in Qb_list:# Iterate over Qb_list quarterbacks.append(Qb(*this_qb)) # append Qb instance to quarterbacks ...or even drop the quarterbacks declaration above... quarterbacks = [Qb(*this_qb) for this_qb in Qb_list Emile 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] basic class loading question
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.mont...@gmail.com,Candlestick Park], [Fran, Tarkington,651-321-7657, frank.tarking...@gmail.com, Metropolitan Stadidum], [Joe, Namath, 212-222-, joe.nam...@gmail.com, Shea Stadium], [John, Elway, 303-9876-333, john.el...@gmai.com, Mile High Stadium], [Archie, Manning, 504-888-1234, archie.mann...@gmail.com, Louisiana Superdome], [Roger, Staubach, 214-765-8989, roger.staub...@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. -- Frank L. Cranky Frankie Palmeri ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] basic class loading question
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.mont...@gmail.com,Candlestick Park], [Fran, Tarkington,651-321-7657, frank.tarking...@gmail.com, Metropolitan Stadidum], [Joe, Namath, 212-222-, joe.nam...@gmail.com, Shea Stadium], [John, Elway, 303-9876-333, john.el...@gmai.com, Mile High Stadium], [Archie, Manning, 504-888-1234, archie.mann...@gmail.com, Louisiana Superdome], [Roger, Staubach, 214-765-8989, roger.staub...@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.mont...@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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] basic class loading question
OK, but this is still not working: 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.mont...@gmail.com,Candlestick Park], [Fran, Tarkington,651-321-7657, frank.tarking...@gmail.com, Metropolitan Stadidum], [Joe, Namath, 212-222-, joe.nam...@gmail.com, Shea Stadium], [John, Elway, 303-9876-333, john.el...@gmai.com, Mile High Stadium], [Archie, Manning, 504-888-1234, archie.mann...@gmail.com, Louisiana Superdome], [Roger, Staubach, 214-765-8989, roger.staub...@gmail.com, Cowboy Stadium]] len_Qb_list = len(Qb_list) for i in range(0, len_Qb_list): quarterbacks = Qb(*Qb_list[i]) i = i + 1 print(quarterbacks.last_name(2)) -- Frank L. Cranky Frankie Palmeri Risible Riding Raconteur Writer “How you do anything is how you do everything.” - from Alabama Crimson Tide training room ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] basic class loading question
On 11/22/2011 09:20 AM, Cranky Frankie wrote: OK, but this is still not working: 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.mont...@gmail.com,Candlestick Park], [Fran, Tarkington,651-321-7657, frank.tarking...@gmail.com, Metropolitan Stadidum], [Joe, Namath, 212-222-, joe.nam...@gmail.com, Shea Stadium], [John, Elway, 303-9876-333, john.el...@gmai.com, Mile High Stadium], [Archie, Manning, 504-888-1234, archie.mann...@gmail.com, Louisiana Superdome], [Roger, Staubach, 214-765-8989, roger.staub...@gmail.com, Cowboy Stadium]] len_Qb_list = len(Qb_list) for i in range(0, len_Qb_list): quarterbacks = Qb(*Qb_list[i]) i = i + 1 print(quarterbacks.last_name(2)) You'll generally get better responses by saying in what way it's not working. In this case, I get an exception in the print statement at the end. So your message ought to state that, and show the traceback. The specific error that causes that exception is you're trying to call a string. What's that (2) all about anyway? quarterbacks is an objext of type Qb, and quarterbacks.last_name is a string. Your variable quarterbacks currently contains an object representing the last line of the list, the one for Roger Staubach. You kept replacing the value in quarterbacks with the next item from the original list. If you actually want a list of the objects, you need to say so. quarterbacks = [] for quarterbacks.append( ) Now that you really have a list, then you can print a particular one with: print (quarterbacks[2].last_name) (I tested my own variant using python 2.7. And i'm not saying your code can't be improved. You're learning, and getting it correct is much more important than getting it optimal.) -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] basic class loading question
On Tue, Nov 22, 2011 at 11:26 AM, Dave Angel d...@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]) 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 -- Frank L. Cranky Frankie Palmeri Risible Riding Raconteur Writer “How you do anything is how you do everything.” - from Alabama Crimson Tide training room ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] basic class loading question
On 11/22/2011 12:09 PM, Cranky Frankie wrote: On Tue, Nov 22, 2011 at 11:26 AM, Dave Angeld...@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, inmodule 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] basic class loading question
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 quarterbacks.append(Qb(*data)) 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... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] basic class loading question
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.mont...@gmail.com,Candlestick Park], [Fran, Tarkington,651-321-7657, frank.tarking...@gmail.com, Metropolitan Stadidum], [Joe, Namath, 212-222-, joe.nam...@gmail.com, Shea Stadium], [John, Elway, 303-9876-333, john.el...@gmai.com, Mile High Stadium], [Archie, Manning, 504-888-1234, archie.mann...@gmail.com, Louisiana Superdome], [Roger, Staubach, 214-765-8989, roger.staub...@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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor