I want to create a module with many attributes (in my case, SI units with prefixes, like mm, cm, km, ...). The obvious way would be to specify them one by one, like
# module.py
mm = "mm"
cm = "cm"
km = "km"
# and so on
Currently I want to have about 600 attributes of this kind, so to me it seems wrong to do it this way. Therefore I was wondering if it is possible to generate those attributes programatically instead. I start with a list of units and a list of prefixes and want to combine each unit with each prefix. As a simple example, consider the following:
# module.py
_prefixes = ["m", "c", "k"]
_names = ["m", "g"]
# Automate this
mm = "mm"
cm = "cm"
km = "km"
mg = "mg"
cg = "cg"
kg = "kg"
If I wanted those to be the attributes of a class, I could use setattr(class, prefix + name, prefix + name), but so far I did not find a way to translate this to module attributes.
I am aware that I could use a dict or something similar instead of attributes, but I want to allow imports like from module import km, which to my knowledge is only possible with direct attributes.
Is there a way to generate those attributes programatically? And if so, should I use it or define the attributes one by one nontheless (for example using a script generating the python code for me)?