如何与Enum和Enum34一起生活?
我注意到Python上的Enum的行为不同。
首先,我很惊讶地注意到这是int
:
>>>import enum
>>>class Color(enum.Enum):
red = 1
>>>Color.red
1
然后我意识到我已经安装了enum
而不是enum34
:
$ sudo apt-get install python-enum34
现在,现在,结果是不同的:
>>>Color.red
<Color.red: 1>
我当前的应用程序是接受enum
我以value.value
获得枚举的值。当然,如果安装了错误的枚举,这将引起例外。
我该如何处理这个问题?
I noticed a different behaviour with enum on Python.
I was first surprised to notice the output for this was an int
:
>>>import enum
>>>class Color(enum.Enum):
red = 1
>>>Color.red
1
Then I realized I had enum
installed instead of enum34
:
$ sudo apt-get install python-enum34
And now, the result is different:
>>>Color.red
<Color.red: 1>
My current application is accepting enum
types where I get the value of the enum with value.value
. Of course this will raise an exception if the wrong enum is installed.
How can I deal with this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
猜测,看起来您有3.4枚举之前存在的
enum
软件包。enum34
之所以命名,是因为已经存在了先前的软件包。enum
和enum34
安装到同一位置,因此将它们共存并不容易 - 此外,它将使您的代码难以分发,就像枚举之一一样位于非标准位置。一种可能性是使用
virtual envs
- 那么您可以安装任何一个venv
中的应用程序需要枚举。As a guess, it looks like you had the
enum
package that existed before the 3.4 Enum came in to being.enum34
is so named because that previous package already existed.Both
enum
andenum34
install to the same location, so making them co-exist is not easy -- plus it would make your code difficult to distribute as one of the enums would be in a non-standard location.One possibility is to use
virtual envs
-- then you can install whichever enum is needed for the application in thevenv
.在另一个答案中调整 @jerry101的评论,这是我所掌握的:
我注意到了:
enum34:
enum .__文件__
==__ init __。pyc
枚举:
enum
。在,可能对他人有帮助。
Adapting @Jerry101's comment in another answer, here's what I landed on:
I noticed that for:
enum34:
enum.__file__
==__init__.pyc
enum:
enum.__file__
==enum.pyc
.I've not heavily used this and it's probably non-ideal, but it suits what I'm looking at and may be helpful for others.