pythonic replacement for enums

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 ci 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?


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'
}

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

链接地址: http://www.djcxy.com/p/91736.html

上一篇: 在Python中设置枚举最简洁的方法是什么?

下一篇: 用于枚举的pythonic替换