Global dictionary or class variables

M

Mr.SpOOn

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?
 
M

Matimus

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
 
F

Fuzzyman

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?

I would *probably* find 'Note.C' more natural to use than
"natural_notes['C']".

Michael Foord
 
A

Arnaud Delobelle

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top