[Tutor] basic class loading question

Emile van Sebille emile at fenx.com
Wed Nov 23 20:17:30 CET 2011


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




More information about the Tutor mailing list