Dynamic Dictionary Creation
David Eppstein
eppstein at ics.uci.edu
Fri Dec 6 14:48:30 EST 2002
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)]
>
> 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.
What's wrong with
def getNoteLen(x): return TicksQ * 4.0 / x
?
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.
--
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/
More information about the Python-list
mailing list