Global dictionary or class variables

Chris Rebert clp at rebertia.com
Fri Oct 24 18:17:01 EDT 2008


On Fri, Oct 24, 2008 at 1:44 PM, Mr. SpOOn <mr.spoon21 at gmail.com> wrote:
> Hi,
> in an application I have to use some variables with fixed valuse.
>
> For example, I'm working with musical notes, so I have a global
> dictionary like this:
>
> natural_notes = {'C': 0, 'D': 2, 'E': 4 ....}
>
> This actually works fine. I was just thinking if it wasn't better to
> use class variables.
>
> Since I have a class Note, I could write:
>
> class Note:
>    C = 0
>    D = 2
>    ...
>
> Which style maybe better? Are both bad practices?

Depends. Does your program use the note values as named constants, or
does it lookup the values dynamically based on the note name?
If the former, then the class is marginally better, though putting the
assignments in a (possibly separate) module would probably be best. If
the latter, than the dictionary is fine.

Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list