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?

Community
  • 1
  • 1
  • 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. – kquinn Feb 08 '09 at 04:09
  • Duplicate: http://stackoverflow.com/questions/36932/whats-the-best-way-to-implement-an-enum-in-python – dF. Feb 08 '09 at 04:10
  • Multi-duplicate: http://stackoverflow.com/search?q=python+enum – Ben Blank Feb 08 '09 at 04:24

2 Answers2

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'
}
pboucher
  • 352
  • 1
  • 10
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...

Arkady
  • 14,305
  • 8
  • 42
  • 46