在Python中如何最好地模仿和/或避免枚举? [英] How should I best emulate and/or avoid enum's in Python?
本文介绍了在Python中如何最好地模仿和/或避免枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一些Python项目中一直使用一个小类来模拟枚举。有没有更好的方法或这对某些情况最有意义?
I've been using a small class to emulate Enums in some Python projects. Is there a better way or does this make the most sense for some situations?
类代码在这里:
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)
推荐答案
有很多很好的讨论这里。
这篇关于在Python中如何最好地模仿和/或避免枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文