0

I have a python module with definitions of constants. They look like so:

30KHZ = 0b000
125KHZ = 0b001
250KHZ = 0b010
1MHZ = 0b011

Obviously, these are not allowed names. One way of to deal with this is to prepend names with something. Names like _30KHZ look nice, but they mess with static analyzers. Names like S_30KHZ or F30KHZ are allowed, but they look awkward and the meaning of the S_ or F may be unclear. Another way is to rotate the name like so: KHZ30. But is doesn't look good either.

How to name such constants to make their meaning obvious to the user?

6
  • 5
    Since these appear to be frequencies, I would opt for the longer name freq_30khz and so on. explicit is better than implicit. Commented Aug 20, 2018 at 15:11
  • 2
    Better yet, are the exact frequencies important, or does the user just need to know that these are, for instance, low, medium, high, and very high frequencies? Commented Aug 20, 2018 at 15:13
  • 3
    I would say FREQ_30HZ, using capital letters and underscores only for constants. Commented Aug 20, 2018 at 15:14
  • @chepner there are about a dozen of those Commented Aug 20, 2018 at 15:16
  • 1
    As an alternative way can you use dict for this constants? Commented Aug 20, 2018 at 15:19

1 Answer 1

2

I would probably put these in a dictionary:

BITMAPS = {
    "30KHZ": 0b000,
    "125KHZ": 0b001,
    "250KHZ": 0b010,
    "1MHZ": 0b011,
}
Sign up to request clarification or add additional context in comments.

3 Comments

arguably even better bitmaps = {30: 0b000, 125: 0b001, 250: 0b010, 1000: 0b011} then you can just access using their value.
For some reason when I think about constants in python, I think "dot notation"
@Sergey: sure. In this case, however, you can't use dot notation since the keys are numbers (or being with numbers), so you have to do something different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.