[Tutor] am I missing another simpler structure?
Kent Johnson
kent37 at tds.net
Thu Dec 16 02:16:47 CET 2004
Brian van den Broek wrote:
> I've begun to wonder if I am overlooking a
> improvement similar to that in DogWalker's suggestion. As an example of
> the sort of thing I have been doing:
>
> import datetime
> def is_leap_year(year):
> '''-> boolean
>
> Returns True or False as year is, or is not, a leap year.
> '''
> is_leap = True
> try:
> datetime.date(year, 2, 29)
> except ValueError:
> is_leap = False
> return is_leap
I would write
def is_leap_year(year):
try:
datetime.date(year, 2, 29)
return True
except ValueError:
return False
Kent
More information about the Tutor
mailing list