Find day of week from month and year
John Machin
sjmachin at lexicon.net
Fri Sep 2 22:12:04 EDT 2005
Peter Hansen wrote:
> 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
An alternative:
def expiration(year, month):
return 21 - (calendar.weekday(year,month,1) + 2) % 7
More information about the Python-list
mailing list