tags:

views:

198

answers:

2

In my python script i am parsing a user created file and typically there will be some errors and there are cases were i warn the user to be more clear. In c i would have an enum like eAssignBad, eAssignMismatch, eAssignmentSignMix (sign mixed with unsigned). Then i would look the value up to print an error or warning msg. I link having the warningMsg in one place and i like the readability of names rather then literal values. Whats a pythonic replacement for this?

Duplicate of: http://stackoverflow.com/questions/36932/whats-the-best-way-to-implement-an-enum-in-python/38092#38092

+3  A: 

You could try making a bunch of exception classes (all subclasses of Exception, perhaps through some common parent class of your own). Each one would have an error message text appropriate for the occasion...

scrible
+3  A: 

Here is one of the best enum implementations I've found so far: http://code.activestate.com/recipes/413486/

But, dare I ask, do you need an enum?

You could have a simple dict with your error messages and some integer constants with your error numbers.

eAssignBad = 0
eAssignMismatch = 1
eAssignmentSignMix = 2

eAssignErrors = {
    eAssignBad: 'Bad assignment',
    eAssignMismatch: 'Mismatched thingy',
    eAssignmentSignMix: 'Bad sign mixing'
}
pboucher