未来兼容的枚举在2.7? [英] Future-compatible enums in 2.7?

查看:108
本文介绍了未来兼容的枚举在2.7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多实现枚举的方法在这个问题的答案中显示。但是, PEP0435 现在已经开始,并描述了Enum和IntEnum如何成为官方在Python 3.4中。

Many methods of implementing enums are shown in the answers to this question. However, PEP0435 is out now and describes how Enum and IntEnum will become official in Python 3.4.

我的问题不在于模拟Python中枚举的特定方式的优点/缺点。相反,我希望对于我们仍然坚持使用2.X版本的最前进的兼容性方式提供建议。

My question is not about the advantages/disadvantages of particular ways of simulating enums in Python. Rather I'd like advice on the most forward-compatibile way for those of us still stuck in 2.X releases.

flufl.enum最初将成为参考实现但由于成员不是类型的实例而被删除(并且同时允许具有其他成员的子类)。后者不会太多的日常影响,但也许前者会导致更多的问题?

flufl.enum was originally going to be the reference implementation but was dropped due to members not being instances of the type (and of allowing subclasses with additional members at the same time). The latter wouldn't affect much day-to-day but perhaps the former would cause more issues?

有没有任何图书馆更加类似于添加的内容到3.4并且与2.7 +?兼容

Are there any libraries out there that more closely resemble what is being added to 3.4 and are compatible with 2.7+?

推荐答案

有一个PEP 435枚举模块的一个backport可用于PyPI,作为 enum34 。 backport似乎是非官方的,但是它是由Ethan Furman撰写的,他是一名Python核心提交者,也是PEP 435的合着者之一。

There is a backport of the PEP 435 enum module available on PyPI as enum34. The backport seems to be unofficial, however it is authored by Ethan Furman, who is a Python core committer and one of the co-authors of PEP 435.

声明与Python 2.4及更高版本兼容。当然,在Python 2中,有一些比较小的行为差异,但是从我在Pyhon 2.7开始的初步探索之前,我会说前向兼容性水平相当高。

The backport is declared compatible with Python 2.4 and greater. Of course, under Python 2 there are a few, relatively minor, differences in behaviour, but from my preliminary exploration under Pyhon 2.7, I'd say that the level of forward compatibility is quite high.

这篇关于未来兼容的枚举在2.7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆