I’m building a Python module with about a hundred constants.
I would like to avoid naming issues when people import my module so I was wondering what would be the best way to do it.
MY_CONSTANT = 1
MY_SECOND_CONSTANT = 2
...
MY2_CONSTANT = "a"
MY2_SECOND_CONSTANT = "b"
...
Or
class My:
CONSTANT = 1
SECOND_CONSTANT = 2
...
class My2
CONSTANT = "a"
SECOND_CONSTANT = "b"
...
Or maybe another of your suggestions?
Coming from Java, I surely prefer the second way, but some might find it overkill…
Well, it depends. Usually, constants are defined at module level. But if you have many constants for
category_aandcategory_b, it might even make sense to add a subpackageconstantswith modulesconstants.category_aandconstants.category_b.I would refrain from using a
class– it could be instantiated, which wouldn’t make sense, and it has no advantage over a module, apart from allowing you to cram more than one into one physical file (which you probably shouldn’t do if there are so many constants). The Java version would probably use a static class, but the Python equivalent is a module.Name clashes aren’t an issue in Python except when you
import *(but you shouldn’t do that anyways: according to the documentation). As long as there are no name clashes inside the module, rest assured that the user will neither pull out all the names from your module into his own, nor import it under a name that clashes with another module.