constants in Python: at the root of the module or in a namespace inside the module?
Question: 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...


Answer: Well, it depends. Usually, constants are defined at module level. But if you
have many constants for `category_a` and `category_b`, it might even make
sense to add a subpackage `constants` with modules `constants.category_a` and
`constants.category_b`.

I would refrain from using a `class` \- it could be instanciated, 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 propably shouldn't
if there are so many constants). The Java version would propably 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 anyway](http://docs.python.org/howto/doanddont.html#from-
module-import). 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.

