This is mainly a "good python style" question.
I have a module which is using a number of constants that feels should be grouped.
Lets say we have Dogs and cat and each of them have number of legs and favorite food.
Note that
- we want to model nothing but those constants about Dogs and Cats
- quite probably we'll have more animals in the future.
- those constants wont be used outside of the current module.
I thought about the following solutions:
Constants at module level
DOG_NUMBER_OF_LEGS = 4
DOG_FAVOURITE_FOOD = ["Socks", "Meat"]
CAT_NUMBER_OF_LEGS = 4
CAT_FAVOURITE_FOOD = ["Lasagna", "Fish"]
They seem not really grouped, but I think it is the solution I prefer.
Classes as namespaces
class Dog(object):
NUMBER_OF_LEGS = 4
DOG_FAVOURITE_FOOD = ["Socks", "Meat"]
class Cat(object):
NUMBER_OF_LEGS = 4
FAVOURITE_FOOD = ["Lasagna", "Fish"]
I don't like this solution as we'll have class that we wont use and they can be actually instantiated.
Dictionary of constants
ANIMALS_CONFIG = {
"DOG" : {
"NUMBER_OF_LEGS" : 4,
"FAVOURITE_FOOD" : ["Socks", "Meat"]
},
"CAT" : {
"NUMBER_OF_LEGS" : 4,
"FAVOURITE_FOOD" : ["Lasagna", "Fish"]
}
}
I also thought about adding submodules but I dont really want to expose those internal constants
what is the most pythonic way to do it / how would you do it?