我一直在使用一个小类来模拟一些 Python 项目中的枚举。有没有更好的方法或者这在某些情况下最有意义?
类代码在这里:
class Enum(object):
'''Simple Enum Class
Example Usage:
>>> codes = Enum('FOO BAR BAZ') # codes.BAZ will be 2 and so on ...'''
def __init__(self, names):
for number, name in enumerate(names.split()):
setattr(self, name, number)