欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python枚举遇到的一个坑

程序员文章站 2024-02-23 18:35:52
...

实现了一个类继承自枚举

from enum import Enum

class tbEmUser(Enum):
    userId = 0
    userName = 1

 

代码的使用是这样的

tbEmUser.userId

结果是:

>>> tbEmUser.userId
0

时候发现换了一台电脑之后

执行同样的代码,结果成了这样

>>> tbEmUser.userId
<tbEmUser.userId: 0>

开始以为是python版本不一致

或者库的版本不一致

 pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
aip (1.0.0.5)
asn1crypto (0.24.0)
baidu-aip (2.0.0.1)
beautifulsoup4 (4.6.0)
blinker (1.4)
bottle (0.12.13)
certifi (2017.11.5)
chardet (3.0.4)
click (6.7)
cryptography (2.1.4)
cssselect (1.0.3)
Cython (0.28.1)
Django (1.11.5)
django-extensions (1.9.9)
django-werkzeug-debugger-runserver (0.3.1)
duplicity (0.7.17)
enum (0.4.7)
enum34 (1.1.6)
eyeD3 (0.8.4)
fasteners (0.12.0)
Flask (0.12.2)
funcsigs (1.0.2)
html5lib (0.999999999)
idna (2.6)
image (1.5.15)
ipaddress (1.0.17)
itsdangerous (0.24)
Jinja2 (2.10)
keyring (10.6.0)
keyrings.alt (3.0)
lockfile (0.12.2)
lxml (4.2.1)
MarkupSafe (1.0)
meld3 (1.0.2)
mercurial (4.5.3)
monotonic (1.0)
mysqlclient (1.3.12)
numpy (1.13.3)
oauthlib (2.0.6)
olefile (0.44)
parsel (1.4.0)
pathlib (1.0.1)
pika (0.9.8)
Pillow (4.2.1)
pip (9.0.1)
PyAudio (0.2.11)
pycairo (1.16.2)
pycrypto (2.6.1)
pycurl (7.43.0.1)
PyDispatcher (2.0.5)
pygame (1.9.1release)
pygobject (3.26.1)
PyJWT (1.5.3)
PyOpenGL (3.1.0)
pyOpenSSL (17.5.0)
pyserial (3.4)
python-magic (0.4.15)
pytz (2017.2)
pyusb (1.0.2)
pyxdg (0.25)
queuelib (1.5.0)
requests (2.18.4)
rope (0.10.5)
scour (0.36)
Scrapy (1.5.0)
SecretStorage (2.3.1)
setuptools (39.0.1)
six (1.11.0)
supervisor (3.3.1)
typing (3.6.2)
unity-lens-photos (1.0)
urllib3 (1.22)
uWSGI (2.0.15)
w3lib (1.19.0)
webencodings (0.5)
Werkzeug (0.14.1)
WeRoBot (1.2.0)
wheel (0.30.0)
xmltodict (0.11.0)
You are using pip version 9.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

查看发现enum的版本都是0.4.7

[email protected]:~$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.

python的版本也都是2.7.15.rc1

这就奇怪了,难道跟系统的版本有关系?因为是从ubuntu16的系统换到ubuntu18的系统执行的

后来仔细看了看库的列表,里面有一个enum34

试着把它卸载掉试试看

[email protected]:~$ sudo pip uninstall enum34
[sudo] password for drivesim: 
The directory '/home/drivesim/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Uninstalling enum34-1.1.6:
  /usr/lib/python2.7/dist-packages/enum
  /usr/lib/python2.7/dist-packages/enum34-1.1.6.egg-info
Proceed (y/n)? y
  Successfully uninstalled enum34-1.1.6
The directory '/home/drivesim/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
You are using pip version 9.0.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

再试了一下,结果居然可以了

那原因估计就是如果既有enum也有enum34这里个库的话,我这样的语法

from enum import Enum

优先导入的是enum34

 

上一篇: Python Xlwings

下一篇: