That's a really dumb question for sure (and perhaps a bit opinion-based), but how do I create an enum-like object that stores things like error codes, which are constant and to be accessed later in code?
It occurs to me that there are four choices:
1) one status code - one variable
STATUS_NEW = 0
STATUS_PENDING = 1
STATUS_DONE = 2
STATUS_ERROR = -1
if some_object.status == STATUS_NEW:
print 'this object is pretty fresh!'
This doesn't look good to me.
2) a dictionary containing all status codes with its name as the key:
STATUS = {'new': 0, 'pending': 1, 'done': 2, 'error': -1}
if some_object.status == STATUS['new']:
print 'you got the idea.'
This looks uglier, but still, one object is better than several ones.
3) a named tuple looks even better, but it's it looks much worse to me at its initialization:
STATUS = namedtuple('Status', ['error', 'new', 'pending', 'done'])
STATUS = STATUS(error=-1, new=0, pending=1, done=2)
if some_object.status == STATUS.new:
print 'looks prettier than using dict!'
4) Using a class:
class Status:
NEW = 0
PENDING = 1
DONE = 2
ERROR = -1
if some_object.status == Status.NEW:
pass
Am I missing something? What is more encouradged?
Thanks in advance!