How to initialize a table of months.

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Apr 16 02:05:29 EDT 2007


In <mailman.6565.1176687041.32031.python-list at python.org>, Steven W. Orr
wrote:

> I want to call datetime.datetim() whose arg2 is a number between 1-12 so I 
> have to convert the month to an integer.
> I wrote this, but I have a sneaky suspicion there's a better way to do it.
> 
> mons = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5, 'Jun':6,
>          'Jul':7, 'Aug':8, 'Sep':9, 'Oct':10, 'Nov':11, 'Dec':12 }
> 
> def mon2int( mon ):
>      global mons
>      return mons[mon]

You've already got some answers, I just want to point out that the
``global`` is unnecessary here and that `mons` as a constant should be
spelled in capital letters by convention.  And maybe it's better to write
`MONTHS` instead the abbreviation.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list