On Oct 24, 1:44*pm, Mr.SpOOn <mr.spoo...@gmail.comwrote:
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?
It really depends on how you plan to use them. I might use a
dictionary if I'm likely be handling the notes as characters. If they
are just constants that I plan to use in my program, I would probably
just define a set of global names. The best practice I have found is a
combination.
NOTES = C,D,E,F,G,A,B = "CDEFGAB"
note_2_step = dict(C=0, D=2, E=4, F=5, G=7, A=9, B=11)
This allows you to do both. There are schemes where you might want to
use a class, but without more information it doesn't really seem
necessary. Globals are frowned upon, but constant tend to be just
fine.
Matt