My module uses constants that I feel should be grouped. Dog and cat have a number of legs and favorite foods.
- I want to model nothing but those constants about dogs and cats.
- Likely I'll have more animals in the future.
- Those constants won't be used outside of the module.
I thought about:
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 grouped, but 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 as they're classes I won't use but 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 don't want to expose those internal constants. What is the most pythonic way or how would you do it?
Animal
withdog
andcat
instances of that class. – Insalivate