Find day of week from month and year
Peter Hansen
peter at engcorp.com
Fri Sep 2 20:53:44 EDT 2005
Carsten Haese wrote:
> On Fri, 2005-09-02 at 16:46, Laguna wrote:
>>def expiration(year, month):
>> weekday = calendar.weekday(year, month, 1)
>> table = [19, 18, 17, 16, 15, 21, 20]
>> return table[weekday]
>>
> This, of course, can be "optimized" into
>
> def expiration(year, month):
> return [19,18,17,16,15,21,20][calendar.weekday(year,month,1)]
>
> ;)
True, but do you find that more readable? If I saw that in code I was
maintaining I would likely rewrite it, probably to look a lot like the
first one (though likely with a more descriptive name than "table"...
maybe expirationTable?).
(And, if I were "optimizing", I would of course dispense with the
dynamic creation of the static table upon every execution of
expiration(), and move it outside the function.)
-Peter
More information about the Python-list
mailing list