Dynamic Dictionary Creation

Carl Banks imbosol at vt.edu
Fri Dec 6 16:18:40 EST 2002


David Eppstein wrote:
> In article <3DF0EE53.62F70405 at kootenay.com>,
> Bob van der Poel <bvdpoel at kootenay.com> wrote:
> 
>> def getNoteLen(x):
>>    global TicksQ
>>    ntb = { '1': TicksQ *
>> 4,                                                               
>>       '2': TicksQ * 2,                                   
>>       '4': TicksQ,
>>       '8': TicksQ
>> }                                                                  
>>                                                   
>>    return ntb[str(x)]  
>>
[snip]
>
> What's wrong with
> 
> def getNoteLen(x): return TicksQ * 4.0 / x
> 
> ?

For one thing, it doesn't match the table.  Also, seeing that he's
generating MIDI files, I doubt he wants to see floats.  (You can
probably get the effect with tricky integer arithmetic, but that
would be much more obfuscated and unclear.)


> If what you are really trying to do is some arithmetic, but you're doing 
> it by converting the input number to a string and doing a table lookup 
> to emulate the arithmetic operations, then trying to optimize the table 
> lookup seems like the wrong place to look for more efficiency.

For this example, yes.  If there's too much more complexity, the table
lookup would be faster, and it would make sense to find more
efficiency there then.


-- 
CARL BANKS



More information about the Python-list mailing list