Sion Arrowsmith wrote:
...
> class State:
>     Enum = range(3)
>     OPENED, CLOSED, ERROR = Enum
>     Names = { OPENED: "OPENED", CLOSED: "CLOSED", ERROR: "ERROR" }

> so you can used State.Names[state] to provide something user-readable, ...

Or use a function like:

def named(value, classes):
     for klass in classes:
         for name, val in vars(klass).iteritems():
             if val == value:
                 return name
     raise ValueError, "No names for %r in %s" (value, classes)

Remember CPU time is almost free when it is associated with a line
typed to a user paying attention.  This way you (A) don't have
to repeat the names in the source (a great place for errors),
and (B) you can say to yourself, "I think this is in one of these
several things" and go hunting happily.

In our example, named(2, [State]) gives us "ERROR"

-- 
-Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to