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
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?
Storing Constants in Configuration Files You may need to keep all your constants out of your project's source code. To do this, you can use an external configuration file. This file uses the INI file format. You can read this type of file using the configparser module from the standard library.
In Python, constants are usually declared and assigned in a module. Here, the module is a new file containing variables, functions, etc which is imported to the main file. Inside the module, constants are written in all capital letters and underscores separating the words.
Constants in Python A constant is a type of variable that holds values, which cannot be changed. In reality, we rarely use constants in Python. Constants are usually declared and assigned on a different module/file.
Python uses four types of constants: integer, floating point, string, and boolean.
I would go for a fourth option, preferring a collections.namedtuple
:
Animal = namedtuple('Animal', 'number_of_legs favourite_food')
You then create instances like:
DOG = Animal(4, ['Socks', 'Meat'])
CAT = Animal(4, ['Lasagna', 'Fish'])
and access the values externally as:
from animals import CAT
print CAT.number_of_legs
There's really no point having classes if you don't need to create any methods, and I think the form of access above is neater than e.g.:
from animals import animals
print animals['CAT']['number_of_legs']
namedtuple
s, like vanilla tuple
s, are immutable, too, so you can't accidentally reassign e.g. CAT.number_of_legs = 2
somewhere.
Finally, the namedtuple
is a lightweight data structure, which may be important if you're creating lots of animals:
>>> import sys
>>> sys.getsizeof({'number_of_legs': 4, 'favourite_food': ['Lasagna', 'Fish']})
140
>>> from collections import namedtuple
>>> Animal = namedtuple('Animal', 'number_of_legs favourite_food')
>>> sys.getsizeof(Animal(4, ['Lasagna', 'Fish']))
36
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With