如何从 python 2.7 中的枚举导入自动?
How to import auto from enum in python 2.7?
我使用的是基于 python 2.7 的旧代码(很遗憾,无法更改)。我必须从基于 python 3.6 的一段代码中引入一个新功能。这段代码可以工作,除非它使用包枚举,据我所知,它不再维护。所以:
from enum import Enum, auto
(不起作用 python 2.7)
因为我认为 python 2.7 中没有定义“auto”。是否有可能使该线路正常工作?或者至少安装/导入“enum.auto”或具有相同功能的东西?
这可能会在紧要关头起作用:
from itertools import count
def auto(it=count()):
return it.next()
不过我还没有测试过。
您有几个选择:
- 改用
aenum
1
- copy/create你自己的
auto()
塞进enum
使用aenum
是一个两步过程:
pip install aenum
- 将
from enum
更改为from aenum
Creating/copying一个auto
然后塞进enum
:
import enum
from itertools import count
def auto(it=count()):
return next(it)
enum.auto = auto
1 披露:我是 Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) 库的作者。
我使用的是基于 python 2.7 的旧代码(很遗憾,无法更改)。我必须从基于 python 3.6 的一段代码中引入一个新功能。这段代码可以工作,除非它使用包枚举,据我所知,它不再维护。所以:
from enum import Enum, auto
(不起作用 python 2.7)
因为我认为 python 2.7 中没有定义“auto”。是否有可能使该线路正常工作?或者至少安装/导入“enum.auto”或具有相同功能的东西?
这可能会在紧要关头起作用:
from itertools import count
def auto(it=count()):
return it.next()
不过我还没有测试过。
您有几个选择:
- 改用
aenum
1 - copy/create你自己的
auto()
塞进enum
使用aenum
是一个两步过程:
pip install aenum
- 将
from enum
更改为from aenum
Creating/copying一个auto
然后塞进enum
:
import enum
from itertools import count
def auto(it=count()):
return next(it)
enum.auto = auto
1 披露:我是 Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) 库的作者。