Dynamic Dictionary Creation

Bob van der Poel bvdpoel at kootenay.com
Fri Dec 6 13:37:07 EST 2002


I'm writing a program which needs to convert note lengths specified in
somewhat standard musical notation to midi ticks. I have a function
which does this with
a dictionary lookup. An abreviated version is shown here:

def getNoteLen(x):
   global TicksQ
   ntb = { '1': TicksQ *
4,                                                               
      '2': TicksQ * 2,                                   
      '4': TicksQ,
      '8': TicksQ
}                                                                  
                                                                                                
   return ntb[str(x)]  

What I'm concerned about is the initialization of the table. Am I
correct in assuming that each time I access the function the values in
the table will be recalculated?

I suppose I could make the table global and avoid this?

I could avoid runtime math by replacing TicksQ with a value, but I've
always been taught not to use literal magic numbers, and python doesn't
have a DEFINE statement.


-- 
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bvdpoel at kootenay.com
WWW:   http://www.kootenay.com/~bvdpoel




More information about the Python-list mailing list