0

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: How can I represent an 'Enum' in Python?

3
  • You probably just want a dict, but it's hard to tell without seeing your entire program/function as pseudocode. C and Python are very different, and the best way to accomplish a task in them is not going to be the same. Commented Feb 8, 2009 at 4:09
  • Duplicate: stackoverflow.com/questions/36932/… Commented Feb 8, 2009 at 4:10
  • Multi-duplicate: stackoverflow.com/search?q=python+enum Commented Feb 8, 2009 at 4:24

2 Answers 2

4

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' } 
Sign up to request clarification or add additional context in comments.

Comments

3

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...

Comments