Global dictionary or class variables

Arnaud Delobelle arnodel at googlemail.com
Sun Oct 26 09:06:15 EDT 2008


On Oct 24, 8:44 pm, Mr.SpOOn <mr.spoo... 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?

You can also put them in a module:

notes.py
========

C = 0
D = 2
....

Then you can:

import notes
print notes.F - notes.C

or

from notes import *

print F - C

If your application consists of several files this may be a good idea.

--
Arnaud




More information about the Python-list mailing list