[Tutor] don't repeat yourself; question about code optimization CORRECTION

Eric Brunson brunson at brunson.com
Sun Jul 29 17:50:35 CEST 2007


tpc247 at gmail.com wrote:
>
>
> On 7/23/07, *Bob Gailer* <bgailer at alum.rpi.edu 
> <mailto:bgailer at alum.rpi.edu>> wrote:
>
>     A correction to the code at the end. The test of
>     self.total_num_of_items
>     should precede the pop(0)
>
>
>
> Bob, I spent today studying what you've been telling me and I put the 
> finishing touches to make your code pass my battery of tests.  It 
> still is repetitive, i.e., table.append(tuple(row)),

I see you're also repeating the use of "=" quite a bit, as well as 
multiple uses of the word "if".  Maybe you could work on that.

Sorry for the sarcasm, but there's a big different between repeating an 
assignment and a type coersion versus a multi line block of code that 
could be converted to a function.  :-)

> but I've found guidance in what Alan had to say about code readability 
> and easy maintenance, and am no longer going to press the matter:
>
> class Table_Creator(object):
>     def __init__(self, total_num_of_items, max_num_of_items_per_row):
>         self.given_items = range(total_num_of_items)
>         self.max_num_of_items_per_row = max_num_of_items_per_row
>
>     def create_grid(self):
>         table = []
>         while True:
>             row = []
>             while len(row) < self.max_num_of_items_per_row:
>                 if not self.given_items:
>                     if row:
>                         table.append(tuple(row))
>                     return table
>                 row.append(self.given_items.pop(0))
>             table.append(tuple(row))
>  
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list