Many (but not all) languages that focus strongly on OOP (C++, C#, Java) have an enum
type for constants rather than putting them in a class. However, in other languages, such as Smalltalk and Python, there is no special construct for constants and it can make sense to put them in a class. To the best of my knowledge, there's no special name for that kind of class.
In other languages, a static class is a class that cannot be instantiated and which defines only constants and static methods. Even though Python has no language-level support for enforcing those rules, I would still refer to a class designed that way as a static class.
In Python 2.6 or greater, you can use a class decorator to enforce the rules:
def staticclass(cls):
"""Decorator to ensure that there are no unbound methods and no instances are
created"""
for name in cls.__dict__.keys():
ob = getattr(cls, name)
if isinstance(ob, types.MethodType) and ob.im_self is None:
raise RuntimeError, "unbound method in class declared no_instances"
def illegal(self):
raise RuntimeError,"Attempt to instantiate class declared no_instances"
cls.__init__ = illegal
return cls
@staticclass
class MyStaticClass(object):
pass
As Manoj points out, many times you can do away with the class and put the constants or functions at the module level. However, there are cases where it really is useful to have a class. For example, if the class has significant state, putting the functions at the module level requires littering the code with global
statements. Although rare, it is also sometimes useful to have a class hierarchy of static classes.
Another alternative to a static class is the singleton pattern, where you ensure that only one instance of the class is every created (and typically provide a static method that returns that instance).