Python Constant Class, Different Enum Implementation So I saw a need for and came up with a class implementation to group constants, which I'm calling a Constant Class. I wondered if there existed other similar implementations or if there is a flaw in what I'm doing. My team is currently finding it fairly useful and thought we should see what other people thought. Also to offer out into the world if it indeed is useful to others.

The need was simple. We wanted to group constants as to allow auto-completion and also the ability to reference a list of all the values for validation. This mainly was for the development of an API backend which handle many groups of different types.

import inspect
import re


class ConstantClass(object):
    """ Container class for meant for grouped constants """

    @classmethod
    def all(cls):
        """
        Returns all constant values in class with the attribute requirements:
          - only uppercase letters and underscores
          - must begin and end with a letter

        """
        regex = r'^[A-Z][A-Z_]*[A-Z]$'
        class_items = inspect.getmembers(cls)
        constants = filter(lambda item: re.match(regex, item[0]), class_items)
        values = map(lambda constant: constant[1], constants)

        return values

class TypeNames(ConstantClass):
    TYPE_A = 'Whatever you want it to be'
    TYPE_B = 'Another example'

TypeNames.TYPE_A # returns 'Whatever you want it to be'
TypeNames.all() # returns list of constant values
TypeNames.TYPE_A in TypeNames.all() # Returns True


Enums the closest thing but it requires we type out EnumClass.CONSTANT.value (I honestly find kind of annoying) and there isn't a built-in function to get all the values. I don't know, maybe I'm splitting hairs here but I'm just curious if there is any draw-back to what I'm doing.
The code looks decent, but there are a couple problems with the overall design:


"constants" can be changed (rebound) or removed from the class
the constants don't have a nice repr() (very useful for debugging)


To solve these problems, and more, check out the aenum library1 -- besides advanced Enum and NamedTuple implementations, it also has a Constant2 class.

Your code above would look like:

from aenum import Constant

class TypeNames(Constant):
    TYPE_A = 'Whatever you want it to be'
    TYPE_B = 'Another example'


and in use:

&gt;&gt;&gt; print(repr(TypeNames.TYPE_A))
&lt;TypeNames.TYPE_A: 'Whatever you want it to be'&gt;

&gt;&gt;&gt; print(TypeNames.TYPE_A)
Whatever you want it to be

&gt;&gt;&gt; try:
...     del TypeNames.TYPE_B
... except AttributeError as exc:
...    print(exc)
....
cannot delete constant &lt;TypeNames.TYPE_B&gt;

&gt;&gt;&gt; try:
...     TypeNames.TYPE_B = 'oops!'
... except AttributeError as exc:
...     print(exc)
...
cannot rebind constant &lt;TypeNames.TYPE_B&gt;




1 Disclosure:  I am the author of the Python stdlib Enum, the enum34 backport, and the Advanced Enumeration (aenum)  library.

2 Looking at your all() implementation I can see that should be added to Constant.