Request a short code review
André
andre.roberge at gmail.com
Thu Apr 17 18:53:36 EDT 2008
On Apr 17, 7:11 pm, ja... at reggieband.com wrote:
> > I am not necessarily looking to make the code shorter or more
> > functional or anything in particular. However if you spot something
> > to improve then I am happy to learn.
>
> To give an example of what I mean I have already altered the code:
>
> def output_random_lesson_of_type(self, type=None):
> """Output a lesson of a specific type.
> If no type is passed in then output any type."""
> output_lessons = self.lesson_data["lessons"]
> if type:
> filtered_lessons = filter(lambda x: x["type"] == type,
> self.lesson_data["lessons"])
> if filtered_lessons:
> output_lessons = filtered_lessons
> else:
> print "Unable to find lessons of type %s." % type
> return self.output_random(output_lessons)
>
> Changes:
> - Respected a column width of 80
> - Created the output_lessons variable, assigned it to a default.
> This remove an else statement and reduced the call to
> self.output_random to a single instance in the return statement
>
> Cheers,
> James
I prefer, especially for longer methods, to return as soon as possible
(thereby indicating that the code below is irrelevant to a particular
condition). Thus, I would replace
output_lessons = filtered lessons
by
return self.output_random(filtered_lessons)
André
More information about the Python-list
mailing list