SRC: Music Synthesizer written in 100% Python

Bernhard Herzog herzog at online.de
Tue Aug 29 09:58:58 EDT 2000


"Jeffrey P Shell" <jeffrey at digicool.com> writes:

> > > def pitchhz(note):
> > >  if note=="A0":
> > >   return 13.75
> > >  if note=="A1":
> > >   return 27.5
> > >  if note=="A2":
> > >   return 55.0
> > >  if note=="A3":
> > >   return 110.0
> >
> >
> > how about something more along the lines of:
> >
> > def pitchhz(note):
> >    return {
> >        "A0" : 13.75,
> >        "A1" : 27.5,
> >        "A2" : 55.0,
> >        ...
> >    }[note]

This solution is not much more efficient than the if-version and for all
I know it might even be slower! The dictionary is constructed everytime
the function is called and then only used to look up one value. Since
the dict is constant, it should be put into a global variable, so that
it's only created once.
 
> Or even:  (for the increasingly_brave):
> 
> pitchhz = {
>     "AO": 13.75,
>     "A1": 27.5,
>     "A2": 55.0
> }.get
> 
> :)

This is much better, if a bit unusual. :)

-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list