Ben Finney wrote: > Howdy all, > > I'd like to have an Abstract Data Type for a scalar value that is > restricted to a small set of values. Like an Enum, I suppose. > > What I would like is to be able to use simple 'str' values in most of > the code, but where the values are actually used in a semantically > meaningful context, have them used as an ADT that will barf if the > value isn't part of the set.
How about this: class BaseEnum(str): """Base class for enumerations. You must subclass and provide values for the _valid list.""" _valid = [] def __init__(self, v): assert self in self._valid, 'value %r not in %r' % (self, self._valid) cass Gender(BaseEnum): _valid = ['male', 'female', 'unknown'] Trying the following: Gender('male') Gender('female') assert Gender('male') == 'male' Gender('shemale') The last will return an error: AssertionError: value 'shemale' not in ['male', 'female', 'unknown'] Graham -- http://mail.python.org/mailman/listinfo/python-list