枚举行为与Python之间的区别

时间:2015-07-30 22:21:15

标签: python heroku enums

这是我的Ubuntu笔记本电脑上的Python 2.7.6:

R

这是Heroku(也是2.7.6):

(myenv)$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> class Fruit(enum.Enum):
...     apple=1
...     orange=2
... 
>>> Fruit.apple
<Fruit.apple: 1>
>>> type(Fruit.apple)
<enum 'Fruit'>

编辑:我的requirements.txt文件包含以下行:

~ $ python
Python 2.7.6 (default, Jul 15 2014, 15:38:10) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> class Fruit(enum.Enum):
...     apple=1
...     orange=2
... 
>>> Fruit.apple
1
>>> type(Fruit.apple)
<type 'int'>

这是合法地导致问题,因为我的生产版本显示本地版本没有的错误!

3 个答案:

答案 0 :(得分:2)

您的ubuntu版本看起来像enum34 backport,而heroku版本是较早的enum版。

如果我正确阅读了您的问题并且enum34的版本工作正常,请更改您的需求文件以使用它。

答案 1 :(得分:1)

$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> enum.__version__
'0.4.4'

尝试检查enum.__version__

答案 2 :(得分:1)

检查枚举库的版本。在您的环境中,可能存在旧的和已弃用的版本。枚举类中的Anyway字段必须是其声明的类型,在您的情况下是int。

...     apple=1
...     orange=2

的类型
fruit = Fruit

将是Fruit