[Tutor] don't repeat yourself; question about code optimization CORRECTION
tpc247 at gmail.com
tpc247 at gmail.com
Sun Jul 29 10:56:19 CEST 2007
On 7/23/07, Bob Gailer <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)), 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))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070729/bc5de4a9/attachment-0001.htm
More information about the Tutor
mailing list